今天在写简单的TCP通讯例子的时候,遇到了一个问题:server 和client能够连接成功,并且client也能够正常发送,但server就是接收不到,在网上搜索一番后,终于解决了问题。在这里整理如下:
大家要注意的是,一个server端可以连接多个client端,server端的accept()函数负责等待并接收client的连接请求,而且accept()函数将不同client端的sockfd作为返回值。为了保证接收到对应的client端数据,所以在client连接成功且使用recv()函数接收数据的时候,recv()函数的第一个参数应该是accept成功后的返回值。
实例代码如下:
1.server端
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#define MAX_MSG_LEN 1024
#define BACKLOG 10
int main(int argc,char *arg[])
{
struct sockaddr_in servAddr,clidAddr;
struct hostent *host = NULL;
int Port = 0,socketFd,sin_size;
socklen_t peerlen;
int recLen = 0;
char buf[MAX_MSG_LEN] = {0};
if(argc<2)
{
printf("please input port number!\r\n");
return -1;
}
// ipV4 TCP 0
if((socketFd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
return -1;
}
printf("socket fd = %d\n",socketFd);
memset(&servAddr,0,sizeof(struct sockaddr_in));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(atoi(arg[1]));
servAddr.sin_addr.s_addr= INADDR_ANY;
if(bind(socketFd,(struct sockaddr *)&servAddr,sizeof(struct sockaddr))==-1)
{
perror("bind:");
}
else
{
printf("bind success \r\n");
}
if(listen(socketFd,BACKLOG)==-1)
{
perror("listen:");
}
else
{
printf("Listening...\r\n");
}
sin_size=sizeof(struct sockaddr_in);
if((socketFd=accept(socketFd,(struct sockaddr *)&clidAddr,&sin_size))==-1)
{
perror("accept:");
return -1;
}
else
{
printf("accept successful!\r\n");
}
while(1)
{
memset(buf,0,sizeof(buf));
if((recLen = recv(socketFd,buf,MAX_MSG_LEN,0))==-1)
{
perror("recv:");
}
else
{
if(recLen>0)
{
recLen = 0;
printf("Receive a message:%s\r\n",buf);
}
}
}
close(socketFd);
return 0;
}
上述代码的关键在于62行:
if((socketFd=accept(socketFd,(struct sockaddr *)&clidAddr,&sin_size))==-1)
recv(socketFd,buf,MAX_MSG_LEN,0);
将sockfd赋值为accept的返回值问题就解决了,为了实验方便,这里一并附上client代码:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#define MAX_MSG_LEN 1024
int main(int argc,char *arg[])
{
struct sockaddr_in servAddr;
struct hostent *host = NULL;
int Port = 0,socketFd;
char buf[MAX_MSG_LEN] = {0};
if(argc<3)
{
printf("please input IP and port number!\r\n");
return -1;
}
if((host = gethostbyname(arg[1]))==NULL)
{
return -1;
}
// ipV4 TCP 0
if((socketFd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
return -1;
}
printf("socket fd = %d\n",socketFd);
memset(&servAddr,0,sizeof(struct sockaddr_in));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(atoi(arg[2]));
servAddr.sin_addr = (*(struct in_addr *)host->h_addr);
if(connect(socketFd,(struct sockaddr *)&servAddr,sizeof(struct sockaddr))==-1)
{
perror("connect:");
return -1;
}
while(1)
{
printf("input:");
scanf("%s",buf);
if(send(socketFd,buf,sizeof(buf),0)==-1)
{
perror("send:");
}
{
printf("send successful!\r\n");
}
memset(buf,0,sizeof(buf));
}
close(socketFd);
return 0;
}