当前位置: 技术问答>linux和unix
怎么实现server和client双向聊天的SOCKET,我哪错了
来源: 互联网 发布时间:2016-04-02
本文导语: server的 #include #include #include #include #include #include #include #include #include #include #include #include #define SERVER_PORT 8100 #define LENGTH_OF_LISTEN_QUEUE 10 int main(int argc,char*argv) { char buff[100],buf[100]; int m,n; int ...
server的
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 8100
#define LENGTH_OF_LISTEN_QUEUE 10
int main(int argc,char*argv)
{
char buff[100],buf[100];
int m,n;
int servfd,clifd;
struct sockaddr_in cliaddr;
servfd=socket(AF_INET,SOCK_STREAM,0);
if(servfd==-1)
{
printf("errno=%dn",errno);
exit(1);
}
else
{
printf("socket create successfullyn");
}
struct sockaddr_in servaddr;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERVER_PORT);
servaddr.sin_addr.s_addr=htons(INADDR_ANY);
bzero(&(servaddr.sin_zero),8);
if(bind(servfd,(struct sockaddr*)&servaddr,sizeof(servaddr))!=0)
{
printf("bind failed"); printf("errno=%dn",errno);
exit(1);
}
else
{
printf("bind successfullyn");}
if(listen(servfd,LENGTH_OF_LISTEN_QUEUE!=0))
{
printf("listen errorn");
exit(1);
}
else
{
printf("listen successfullyn");
}
while(1)
{
socklen_t len=sizeof(cliaddr);
clifd=accept(servfd,(struct sockaddr *)&cliaddr,&len);
if(clifd>0)
{
printf("accept successfullyn");
}
printf("from client,IP:%s,Port:%dn",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
int closing=0;
while(closing==0)
{
while((n=recv(clifd,buff,100,0))>0)
{
printf("number of receive types=%dn",n);
write(STDOUT_FILENO,buff,n);
break;
}
printf("Input buffn");
scanf("%s",&buf);
send(clifd, buf, strlen(buf), 0);
buf[101] ='';
if(strcmp(buff,"close")==0)
{
closing=1;
printf("client is closingn");
break;
}
}
close(clifd);
close(servfd);
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 8100
#define LENGTH_OF_LISTEN_QUEUE 10
int main(int argc,char*argv)
{
char buff[100],buf[100];
int m,n;
int servfd,clifd;
struct sockaddr_in cliaddr;
servfd=socket(AF_INET,SOCK_STREAM,0);
if(servfd==-1)
{
printf("errno=%dn",errno);
exit(1);
}
else
{
printf("socket create successfullyn");
}
struct sockaddr_in servaddr;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERVER_PORT);
servaddr.sin_addr.s_addr=htons(INADDR_ANY);
bzero(&(servaddr.sin_zero),8);
if(bind(servfd,(struct sockaddr*)&servaddr,sizeof(servaddr))!=0)
{
printf("bind failed"); printf("errno=%dn",errno);
exit(1);
}
else
{
printf("bind successfullyn");}
if(listen(servfd,LENGTH_OF_LISTEN_QUEUE!=0))
{
printf("listen errorn");
exit(1);
}
else
{
printf("listen successfullyn");
}
while(1)
{
socklen_t len=sizeof(cliaddr);
clifd=accept(servfd,(struct sockaddr *)&cliaddr,&len);
if(clifd>0)
{
printf("accept successfullyn");
}
printf("from client,IP:%s,Port:%dn",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
int closing=0;
while(closing==0)
{
while((n=recv(clifd,buff,100,0))>0)
{
printf("number of receive types=%dn",n);
write(STDOUT_FILENO,buff,n);
break;
}
printf("Input buffn");
scanf("%s",&buf);
send(clifd, buf, strlen(buf), 0);
buf[101] ='';
if(strcmp(buff,"close")==0)
{
closing=1;
printf("client is closingn");
break;
}
}
close(clifd);
close(servfd);
}
return 0;
}
|
while((m=clifd(servfd,buf,100,0))>0)
这是什么?
两个输入源,STDIN和socket,需要用select,或者多线程。
|
你可以考虑使用SELECT方式来实现,不然在读时会等待而僵死ing