粘包问题的简单简单解决:定义整个数据包(包头+包体)
粘包问题的产生: 当然还有别的原因tcp拥塞,等等情况。
解决粘包的代码
客户端:
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#define ERR_EXIT(m)do{perror(m);exit(EXIT_FAILURE);}while(0)
struct packet
{
int len;
char buf[1024];
};
ssize_t readn(int fd, void *buf, size_t count)
{
size_t nleft = count;//剩余的字节数
size_t nread;//接收的字节数
char * bufp = (char *)buf;
while(nleft > 0)
{
if((nread = read(fd,bufp,nleft)) < 0)
{
if(errno == EINTR)//信号中断
continue;
return -1;
}
else if(nread == 0) //客户端关闭了
{
return count - nleft;
}
bufp += nread;
nleft -=nread;
}
return count;
}
ssize_t writen(int fd, const void *buf, size_t count)
{
size_t nleft = count;//剩余的字节数
size_t nwritten;//接收的字节数
char * bufp = (char *)buf;
while(nleft > 0)
{
if((nwritten = write(fd,bufp,nleft)) < 0)
{
if(errno == EINTR)//信号中断
continue;
return -1;
}
else if(nwritten == 0) //客户端关闭了
{
continue;
}
bufp += nwritten;
nleft -=nwritten;
}
return count;
}
int main(void)
{
int sock;
if((sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) <0)
{
ERR_EXIT("socket");
}
struct sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5188);
//servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
//inet_aton("127.0.0.1",&servaddr.sin_addr);
if(connect(sock,(struct sockaddr*)&servaddr,sizeof(servaddr)) < 0)
{
ERR_EXIT("connect");
}
struct packet sendbuf;
struct packet recvbuf;
memset(&sendbuf,0,sizeof(sendbuf));
memset(&recvbuf,0,sizeof(recvbuf));
int n;
while(fgets(sendbuf.buf,sizeof(sendbuf.buf),stdin) != NULL)
{
n = strlen(sendbuf.buf);
sendbuf.len = htonl(n);
writen(sock,&sendbuf,4+n);
//readn(sock,recvbuf,sizeof(recvbuf));
int ret = readn(sock,&recvbuf.len,4);//接收包头
if(ret == -1)
{
ERR_EXIT("read");
}
else if(ret < 4)//客户端关闭
{
printf("client close\n");
break;
}
n = ntohl(recvbuf.len);
ret = readn(sock,recvbuf.buf,n) ;//接收包体
if(ret == -1)
{
ERR_EXIT("read");
}
else if(ret < n)//客户端关闭
{
printf("client close\n");
break;
}
fputs(recvbuf.buf,stdout);
memset(&sendbuf,0,sizeof(sendbuf));
memset(&recvbuf,0,sizeof(recvbuf));
}
close(sock);
return 0;
}
服务端:
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#define ERR_EXIT(m)do{perror(m);exit(EXIT_FAILURE);}while(0)
struct packet
{
int len;
char buf[1024];
};
ssize_t readn(int fd, void *buf, size_t count)
{
size_t nleft = count;//剩余的字节数
size_t nread;//接收的字节数
char * bufp = (char *)buf;
while(nleft > 0)
{
if((nread = read(fd,bufp,nleft)) < 0)
{
if(errno == EINTR)//信号中断
continue;
return -1;
}
else if(nread == 0) //客户端关闭了
{
return count - nleft;
}
bufp += nread;
nleft -=nread;
}
return count;
}
ssize_t writen(int fd, const void *buf, size_t count)
{
size_t nleft = count;//剩余的字节数
size_t nwritten;//接收的字节数
char * bufp = (char *)buf;
while(nleft > 0)
{
if((nwritten = write(fd,bufp,nleft)) < 0)
{
if(errno == EINTR)//信号中断
continue;
return -1;
}
else if(nwritten == 0) //客户端关闭了
{
continue;
}
bufp += nwritten;
nleft -=nwritten;
}
return count;
}
//多个客户端可以连接,将accept弄作一个进程,读取发送数据是一个进程
void do_service(int conn)
{
struct packet recvbuf;
int n;
while(1)
{
memset(&recvbuf,0,sizeof(recvbuf));
int ret = readn(conn,&recvbuf.len,4);//接收包头
if(ret == -1)
{
ERR_EXIT("read");
}
else if(ret < 4)//客户端关闭
{
printf("client close\n");
break;
}
n = ntohl(recvbuf.len);
ret = readn(conn,recvbuf.buf,n) ;//接收包体
if(ret == -1)
{
ERR_EXIT("read");
}
else if(ret < n)//客户端关闭
{
printf("client close\n");
break;
}
fputs(recvbuf.buf,stdout);
writen(conn,&recvbuf,4+n);
}
}
int main(void)
{
int listenfd;
if((listenfd = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) <0)
//listenfd = socket(PF_INET,SOCK_STREAM,0);
{
ERR_EXIT("socket");
}
struct sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5188);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
//servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
//inet_aton("127.0.0.1",&servaddr.sin_addr);
//开启地址重复利用(在TIME_WAIT未消失的情况下,允许服务器重启)
int on = 1;
if(setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)) < 0)
ERR_EXIT("setsockopt");
if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) < 0)
ERR_EXIT("bind");
if(listen(listenfd,SOMAXCONN) < 0)
ERR_EXIT("listen");
struct sockaddr_in peeraddr;
socklen_t peerlen = sizeof(peeraddr);
int conn;
pid_t pid;
while(1)
{
if((conn = accept(listenfd,(struct sockaddr*)&peeraddr, &peerlen)) < 0)
ERR_EXIT("accept");
printf("ip=%s port=%d\n",inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port));
pid = fork();
if(pid == -1)
ERR_EXIT("fork");
if(pid == 0)
{
close(listenfd);
do_service(conn);
exit(EXIT_SUCCESS);
}
else
{
close(conn);
}
}
return 0;
}
这里只是简单demo。
本文由 Ryan 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:
2020/05/11 14:28