socket网络编程快速上手(二)——细节问题(2)

2023-05-29,,

2.TCP数据包接收问题

对初学者来说,很多都会认为:客户端与服务器最终的打印数据接收或者发送条数都该是一致的,1000条发送打印,1000条接收打印,长度都为1000。但是,事实上并不是这样,发送打印基本不会有什么问题(只是一般情况,如果发生调度或者其他情况,有可能导致差别,因此也要注意封装),接收打印却不是固定的,下面是测试代码:

测试客户端程序:

 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> #define PORT 1234
#define MAXDATASIZE 1000 int main(int argc, char *argv[])
{
int sockfd, num;
char buf[MAXDATASIZE + ] = {};
struct sockaddr_in server;
int iCount = ; if (argc != )
{
printf("Usage:%s <IP Address>\n", argv[]);
exit();
} if ((sockfd=socket(AF_INET, SOCK_STREAM, )) == -)
{
printf("socket()error\n");
exit();
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = inet_addr(argv[]);
if (connect(sockfd, (struct sockaddr *)&server, sizeof(server)) == -)
{
printf("connect()error\n");
exit();
} while ()
{
memset(buf, , sizeof(buf));
if ((num = recv(sockfd, buf, MAXDATASIZE,)) == -)
{
printf("recv() error\n");
exit();
}
buf[num - ]='\0';
printf("%dth Recv Length: %d\n", iCount++, num);
} close(sockfd); return ;
}

TCP客户端

测试服务器程序:

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h> #define PORT 1234
#define BACKLOG 5
#define MAXDATASIZE 1000 int main()
{
int listenfd, connectfd;
struct sockaddr_in server;
struct sockaddr_in client;
socklen_t addrlen;
char szbuf[MAXDATASIZE] = {};
int iCount = ;
int iLength = ; if ((listenfd = socket(AF_INET, SOCK_STREAM, )) == -)
{
perror("Creating socket failed.");
exit();
} int opt = SO_REUSEADDR;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -)
{
perror("Bind()error.");
exit();
}
if (listen(listenfd, BACKLOG) == -)
{
perror("listen()error\n");
exit();
} addrlen = sizeof(client);
if ((connectfd = accept(listenfd, (struct sockaddr*)&client, &addrlen)) == -)
{
perror("accept()error\n");
exit();
}
printf("You got a connection from cient's ip is %s, prot is %d\n", inet_ntoa(client.sin_addr), htons(client.sin_port)); memset(szbuf, 'a', sizeof(szbuf));
while (iCount < )
{
iLength = send(connectfd, szbuf, sizeof(szbuf), );
printf("%dth Server Send Length %d\n", iCount++, iLength);
} printf("send over!\n");
sleep(); close(connectfd);
close(listenfd); return ;
}

TCP服务器程序

客户端接收打印片段如下:

 936th Recv Length:
937th Recv Length:
938th Recv Length:
939th Recv Length:
940th Recv Length:
941th Recv Length:
942th Recv Length:
943th Recv Length:
944th Recv Length:
945th Recv Length:
946th Recv Length:
947th Recv Length:
948th Recv Length:
949th Recv Length:
950th Recv Length:
951th Recv Length:
952th Recv Length:
953th Recv Length:
954th Recv Length:
955th Recv Length:
956th Recv Length:
957th Recv Length:
958th Recv Length:
959th Recv Length:
960th Recv Length:
961th Recv Length:
962th Recv Length:
963th Recv Length:
964th Recv Length:
965th Recv Length:
966th Recv Length:
967th Recv Length:
968th Recv Length:
969th Recv Length:
970th Recv Length:
971th Recv Length:
972th Recv Length:
973th Recv Length:
974th Recv Length:
975th Recv Length:
976th Recv Length:
977th Recv Length:
978th Recv Length:
979th Recv Length:
980th Recv Length:
981th Recv Length:
982th Recv Length:
983th Recv Length:
984th Recv Length:
985th Recv Length:
986th Recv Length:
987th Recv Length:
988th Recv Length:
989th Recv Length:
990th Recv Length:
991th Recv Length:
992th Recv Length:
993th Recv Length:
994th Recv Length:
995th Recv Length:
996th Recv Length:
997th Recv Length:
998th Recv Length:
999th Recv Length:
1000th Recv Length:
1001th Recv Length:
1002th Recv Length:
1003th Recv Length:
1004th Recv Length:
1005th Recv Length:
1006th Recv Length:
1007th Recv Length:
1008th Recv Length:
1009th Recv Length:
1010th Recv Length:
1011th Recv Length:
1012th Recv Length:
1013th Recv Length:
1014th Recv Length:
1015th Recv Length:
1016th Recv Length:
1017th Recv Length:
1018th Recv Length:
1019th Recv Length:
1020th Recv Length:
1021th Recv Length:
1022th Recv Length:
1023th Recv Length:
1024th Recv Length:
1025th Recv Length:
1026th Recv Length:
1027th Recv Length:
1028th Recv Length:
1029th Recv Length:
1030th Recv Length:
1031th Recv Length:
1032th Recv Length:
1033th Recv Length:
1034th Recv Length:
1035th Recv Length:
1036th Recv Length:
1037th Recv Length:
1038th Recv Length:
1039th Recv Length:
1040th Recv Length:
1041th Recv Length:
1042th Recv Length:
1043th Recv Length:
1044th Recv Length:
1045th Recv Length:
1046th Recv Length:
1047th Recv Length:
1048th Recv Length:
1049th Recv Length:
1050th Recv Length:

客户端接收打印片段

服务器发送打印片段整理时发现丢失了,大家可以自己试试,没有问题。

不难发现,服务器发送正常,客户端在接收时却和我们想的很不一样,但发送和接收的总数据量是一致的,就是说数据没有丢失。如果编程者认为TCP情况下发送和接收的数据长度都一致的,那就极有可能在代码中体现出这一思想,最终出现问题。

其实,这就是所谓的“粘包”现象,Stevens很明确地已经指出了这一点,他说,“UDP是长度固定的、无连接的不可靠报文传输;TCP是有序、可靠、双向的面向连接字节流”。他没说TCP是长度固定的,有没有?当然我更倾向于这样的理解,UDP是面向报文的,报文在传输时是不能被分割的(只是从应用层来看);TCP是面向字节流的,接收多少数据完全取决于发送和接收的速度了,有多少数据recv就返回多少,数据长度并不和send保持一致,也没这个必要。

那么这个问题怎么解决呢?其实,我们只要将recv封装一层就可以了,那就是我们熟悉的readn函数(该函数不是系统调用),代码如下:

 int readn(int connfd, void *vptr, int n)
{
int nleft;
int nread;
char *ptr;
struct timeval select_timeout;
fd_set rset; ptr = vptr;
nleft = n; while (nleft > )
{
FD_ZERO(&rset);
FD_SET(connfd, &rset);
select_timeout.tv_sec = ;
select_timeout.tv_usec = ;
if (select(connfd+, &rset, NULL, NULL, &select_timeout) <= )
{
return -;
}
if ((nread = recv(connfd, ptr, nleft, )) < )
{
if(errno == EINTR)
{
nread = ;
}
else
{
return -;
}
}
else if (nread == )
{
break;
}
nleft -= nread;
ptr += nread;
}
return(n - nleft);
}

readn

相应的也有writen函数

 int writen(int connfd, void *vptr, size_t n)
{
int nleft, nwritten;
char *ptr; ptr = vptr;
nleft = n; while(nleft>)
{
if((nwritten = send(connfd, ptr, nleft, )) == ERROR)
{
if(errnoGet() == EINTR)
{
//PRT_ERR(("EINTR\n"));
nwritten = ;
}
else
{
//PRT_ERR(("Send() error, 0x%x\n", errnoGet()));
return ERROR;
}
}
nleft -= nwritten;
ptr += nwritten;
} return(n);
}

writen

函数中为什么对EINTR进行处理后面再说,也是必不可少的。

在处理TCP发送和接收部分时,可以说必须要使用上述封装,否则等到造成数据不完整或者不一致后再去找问题,可能就麻烦了。这个是必不可少滴。

socket网络编程快速上手(二)——细节问题(2)的相关教程结束。

《socket网络编程快速上手(二)——细节问题(2).doc》

下载本文的Word格式文档,以方便收藏与打印。