tcp傳送的是流所以可能產生下圖幾種粘包問題
傳過來的可能是 m1包加m2包
m1包加m2包前半部分
m1包後半部分加m2包
粘包產生的原因:
解決方案:
本質上是要在應用層維護訊息與訊息的邊界
定長包包尾加\r\n(ftp)
包頭加上包體長度
更複雜的應用層協議
1、我們可以封裝2個函式 readn 和 writen 確定讀和寫的長度
server
#include #include #include #include #include #include #include #include #include #define err_exit(m) do while(0)
struct packet;
ssize_t readn(int fd,void *buf,size_t count)
else if(nread==0)
return count-nleft;
/*nread>0*/
bufp+=nread;
nleft-=nread;
} return count;
}ssize_t writen(int fd,const void*buf,size_t count)
else if(nwritten==0)
continue;
bufp+=nwritten;
nleft-=nwritten;
}return count;
}void do_service(int conn)
n=ntohl(recvbuf.len);
ret=readn(conn,recvbuf.buf,n);
if(ret==-1)
err_exit("read");
else if(retclient:
#include #include #include #include #include #include #include #include #include #define err_exit(m) do while(0)
struct packet;
ssize_t readn(int fd,void *buf,size_t count)
else if(nread==0)
return count-nleft;
/*nread>0*/
bufp+=nread;
nleft-=nread;
} return count;
}ssize_t writen(int fd,const void*buf,size_t count)
else if(nwritten==0)
continue;
bufp+=nwritten;
nleft-=nwritten;
}return count;
}void do_service(int conn)
n=ntohl(recvbuf.len);
ret=readn(conn,recvbuf.buf,n);
if(ret==-1)
err_exit("read");
else if(ret2、包尾加\r\n readline函式
server:
#include #include #include #include #include #include #include #include #include #define err_exit(m) do while(0)
ssize_t readn(int fd,void *buf,size_t count)
else if(nread==0)
return count-nleft;
/*nread>0*/
bufp+=nread;
nleft-=nread;
} return count;
}ssize_t writen(int fd,const void*buf,size_t count)
else if(nwritten==0)
continue;
bufp+=nwritten;
nleft-=nwritten;
}return count;
}ssize_t recv_peek(int sockfd,void *buf,size_t len)
}ssize_t readline(int sockfd,void *buf,size_t maxline)
return -1;
}void do_service(int conn)
fputs(recvbuf,stdout);
writen(conn,recvbuf,strlen(recvbuf));
}}int main(void)
else
close(conn);
} return 0;
}
client:
#include #include #include #include #include #include #include #include #include #define err_exit(m) do while(0)
size_t readn(int fd,void *buf,size_t count)
else if(nread==0)
return count-nleft;
/*nread>0*/
bufp+=nread;
nleft-=nread;
}return count;
}ssize_t writen(int fd,const void*buf,size_t count)
else if(nwritten==0)
continue;
bufp+=nwritten;
nleft-=nwritten;
}return count;
}ssize_t recv_peek(int sockfd,void *buf,size_t len)
}ssize_t readline(int sockfd,void *buf,size_t maxline)
return -1;
}int main(void);
char recvbuf[1024]=;
while(fgets(sendbuf,sizeof(sendbuf),stdin)!=null)
fputs(recvbuf,stdout);
memset(sendbuf,0,sizeof(sendbuf));
memset(recvbuf,0,sizeof(recvbuf));
} close(sock);
return 0;
}
TCP 回射伺服器
tcp reflect server client tcp回射伺服器。學習了 unp 的第五章前面的知識,自己把 敲出來了,加深了理解吧。簡單地說就是,client傳送給server一條訊息 一行文字 server再將同樣地訊息傳送回client。就像這樣 用到的函式和api包括 1 socket ...
Linux C 回射伺服器
回射伺服器就是服務端將客戶端的資料傳送回去。我實現的回射伺服器返回增加了時間。服務端 可以很容易看懂 cpp view plain copy include include include include include include include include include define ...
TCP 回射伺服器
本例為多程序的 tcp 回射程式 服務端 include intmain int argc,char ar close connfd 父程序斷開連線 void str echo int sockfd 套接字 sockaddr in cliaddr,servaddr 網際套接字位址結構,包含協議族 i...