如何编写程序与客户端和服务器进行通信 [英] how to write program to communicate client and server
本文介绍了如何编写程序与客户端和服务器进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C ++中,我正在Ubuntu环境中进行套接字编程,
在这个阶段我正在尝试,
客户端:
in c++, i m doing socket programming in Ubuntu environment,
at this stage i m trying this,
client side:
do
{
printf("Please enter the message:(Enter q for quit) ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
if(buffer[0]=='q')
break;
printf("%s\n",buffer);
}while(1) ;
服务器端:
server side:
do{
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
if(buffer[0]=='q')
break;
printf("Please enter the message:(Enter q for quit) ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(newsockfd,buffer,strlen(buffer));
if (n < 0) error("ERROR writing to socket");
}while(1);
现在每个人都可以发送一条消息,然后等待响应以发送更多消息,
我想制作一个客户端服务器,以便他们可以一次通过多条线聊天.就像Yahoo和Facebook聊天一样.
请事先给我一些建议..thanx ....
now each one can send one message then wait for response to send more messages,
i want to make a client server , so that they can chat through multiple lines at once. just like, yahoo and facebook chats..
plz suggest me something.. thanx in advance....
推荐答案
请参阅以下内容:
C ++中的轻量级客户端/服务器套接字类
See this one :
A light-weighted client/server socket class in C++
如果您使用的是Boost,则可以使用
http://www.pion.org/projects/pion-network-library [ ^ ]
或
http://pocoproject.org/ [ ^ ]
他们也有很多例子.
If you are using Boost you can use
http://www.pion.org/projects/pion-network-library[^]
or
http://pocoproject.org/[^]
They have many examples also.
这篇关于如何编写程序与客户端和服务器进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文