1. TCP 为什么会有粘包问题?
TCP 是一种 流式连接,不会区分不同数据报之间的界限。
2. 如何解决呢?
2.1 可以使用固定长度发送数据包
2.2 在包头加上包体长度
2.3 使用应用层协议来解决粘包?
2.4 你的系统是怎么解决粘包问题的???
3. why I cannot get the manual help info when running command “man read” or “man write” in my linux system.
reason is: you didn’t install the manual help document in you system.
how to solve:
step 1: download the manual document.
https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/
step 2: run “make install”
- following code is an example of sending/receiving data with fixed length(2.1)
tcp server:
/*description:
1. tcp server: receive data with fixed length from client
2. send the received data back to client */
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include<errno.h>
ssize_t readn(int fd, void *buf, size_t count)
{
int n_left = count;
int n_read;
char * readbuf = (char *)buf;
while (n_left > 0)
{
if ((n_read = read(fd, readbuf, n_left)) < 0)
{
if (errno == EINTR)
continue;
return -1;
}
else if (n_read == 0)
{
printf ("client has closed\n");
break;
}
printf ("1. receive data is %s, length is %d,lef data:%d\n", readbuf, n_read, n_left - n_read);
readbuf += n_read;
n_left -= n_read;
printf("left data is %d", n_left);
}
return count - n_left;
}
ssize_t writen(int fd, const void *buf, size_t count)
{
int n_left = count;
int n_written;
char * writebuf = (char *)buf;
while (n_left > 0)
{
if ((n_written = write(fd, writebuf, n_left)) < 0)
{
if (errno == EINTR)
continue;
return -1;
}
else if (n_written == 0)
{
continue;
}
writebuf += n_written;
n_left -= n_written;
}
return count - n_left;
}
int main()
{
int listenfd = 0;
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error\n");
exit(1);
}
printf("creat socket success\n");
struct sockaddr_in seraddr;
memset(&seraddr, 0, sizeof(seraddr));
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(51888);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (bind(listenfd, (struct sockaddr *)&seraddr,sizeof(seraddr)) < 0)
{
perror("bind error\n");
exit(1);
}
printf("bind success\n");
if (listen(listenfd, 50) < 0)
{
perror("listen error\n");
exit(1);
}
printf("listen success\n");
struct sockaddr_in peer_addr;
socklen_t addr_len = sizeof(peer_addr);
memset(&peer_addr, 0, sizeof(peer_addr));
int conid;
if ((conid = accept(listenfd, (struct sockaddr *)&peer_addr, &addr_len)) < 0)
{
perror("listen error\n");
exit(1);
}
printf("accept success\n");
printf("peer address is %s, port is %d\n", inet_ntoa(peer_addr.sin_addr),ntohs(peer_addr.sin_port));
char recbuf[50];
while (1)
{
memset(recbuf, 0, sizeof(recbuf));
int ret = readn(conid, recbuf, sizeof(recbuf));
if (ret == 0)
{
break;
}
printf ("receive data is %s, length is %d\n", recbuf, ret);
writen(conid, recbuf, ret);
printf ("send data is %s to client,length is %d\n", recbuf, ret);
}
close(listenfd);
close(conid);
return 0;
}
tcp client:
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
ssize_t readn(int fd, void *buf, size_t count)
{
int n_left = count;
int n_read;
char * readbuf = (char *)buf;
while (n_left > 0)
{
if ((n_read = read(fd, readbuf, n_left)) < 0)
{
if (errno == EINTR)
continue;
return -1;
}
else if (n_read == 0)
{
printf ("client has closed\n");
break;
}
printf ("1. receive data is %s, length is %d,lef data:%d\n", readbuf, n_read, n_left - n_read);
readbuf += n_read;
n_left -= n_read;
printf("left data is %d", n_left);
}
return count - n_left;
}
ssize_t writen(int fd, const void *buf, size_t count)
{
int n_left = count;
int n_written;
char * writebuf = (char *)buf;
while (n_left > 0)
{
if ((n_written = write(fd, writebuf, n_left)) < 0)
{
if (errno == EINTR)
continue;
return -1;
}
else if (n_written == 0)
{
continue;
}
writebuf += n_written;
n_left -= n_written;
}
return count - n_left;
}
int main()
{
int confd = 0;
if((confd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error\n");
exit(1);
}
printf("create socket success\n");
struct sockaddr_in seraddr;
memset(&seraddr, 0, sizeof(seraddr));
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(51888);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(confd, (struct sockaddr *)&seraddr, sizeof(seraddr)) < 0)
{
perror("connect error\n");
exit(1);
}
printf ("connect to server success\n");
char sendbuf[50] = {0};
char recbuf[50] = {0};
while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL)
{
writen(confd, sendbuf, sizeof(sendbuf));
readn(confd, recbuf, sizeof(recbuf));
printf ("receive data:%s\n", recbuf);
memset(sendbuf, 0, sizeof(sendbuf));
memset(recbuf, 0, sizeof(recbuf));
}
close(confd);
return 0;
}
这种发送定长的 方案有一个弊端就是浪费带宽。无论我发送的数据大小与否 每次都需要发送定长的数据包。
所以,首选的方案是是2.2,即在包头 加上包体的长度,这种方式的实现在这里:包头部添加包体长度解决TCP粘包问题
或者对于,使用应用层添加逻辑来处理粘包问题,参见事例:应用层处理TCP粘包问题事例