欢迎来到天天文库
浏览记录
ID:39468724
大小:110.50 KB
页数:21页
时间:2019-07-04
《tcp三次握手的资料》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、次我用winpcap成功发送一个UDP数据包到服务器。相比较TCP要复杂的多,tcp不但要完成开始连接的三次握手,还要处理数据包的序列号。下面是tcp七次握手示意图:下面我就来模拟一个TCP客服端,发送一个连接到服务器,服务器之后断开。这样就是7次握手,不含其他数据包。首先我们做一个tcp服务器,最好是一个公网的,这样连接就更有真实性。服务器代码:viewplaincopytoclipboardprint?#include#include#include#include2、lib.h>#pragmacomment(lib,"ws2_32.lib")SOCKETtcpServerSocket=0;SOCKADDR_INtcpServerAddr={0};constintbacklog=SOMAXCONN;WSADATAwsaData={0};intret_code;#definePORT7456//serverport#defineIPSTRING"122.*.*.*"//serverip//³ö´í´¦Àíº¯Êývoiderror(constchar*errstr,interrcode){printf("E3、rror:%s",errstr);printf("ErrorCode:%s",errcode);}voiderror_exit(constchar*errstr,interrcode){error(errstr,errcode);exit(errcode);}voidmain(){ret_code=WSAStartup(MAKEWORD(2,2),&wsaData);if(ret_code!=0){error_exit("WSAStartuperror",ret_code);}//´´½¨Ì×½Ó×Öif((tcpServerSoc4、ket=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET)error_exit("socketerror",WSAGetLastError());//°ó¶¨IPºÍ¶Ë¿ÚtcpServerAddr.sin_family=AF_INET;tcpServerAddr.sin_port=htons(PORT);tcpServerAddr.sin_addr.S_un.S_addr=inet_addr(IPSTRING);if((bind(tcpServerSocket,(structsockaddr*5、)&tcpServerAddr,sizeof(tcpServerAddr)))==SOCKET_ERROR)error_exit("binderror",WSAGetLastError());if((listen(tcpServerSocket,backlog))==SOCKET_ERROR)error_exit("listenerror",WSAGetLastError());while(true){SOCKETtcpClientSocket=0;SOCKADDR_INtcpClientAddr={0};inttcpClientAddrL6、en=sizeof(SOCKADDR_IN);if((tcpClientSocket=accept(tcpServerSocket,(structsockaddr*)&tcpClientAddr,&tcpClientAddrLen))==INVALID_SOCKET){error_exit("accepterror",WSAGetLastError());continue;}printf("FromIP=%sPort=%d",inet_ntoa(tcpClientAddr.sin_addr),ntohs(tcpClientAddr.si7、n_port));//·¢ËͺͽÓÊÕÊý¾Ý//recv//sendclosesocket(tcpClientSocket);}}#include#include#include#include#pragmacomment(lib,"ws2_32.lib")SOCKETtcpServerSocket=0;SOCKADDR_INtcpServerAddr={0};constintbacklog=SOMAXCONN;WSADATAwsaData={0};i8、ntret_code;#definePORT7456//serverport#defineIPSTRING"122.*.*.*"//serverip//³ö´í´¦Àíº¯Êýv
2、lib.h>#pragmacomment(lib,"ws2_32.lib")SOCKETtcpServerSocket=0;SOCKADDR_INtcpServerAddr={0};constintbacklog=SOMAXCONN;WSADATAwsaData={0};intret_code;#definePORT7456//serverport#defineIPSTRING"122.*.*.*"//serverip//³ö´í´¦Àíº¯Êývoiderror(constchar*errstr,interrcode){printf("E
3、rror:%s",errstr);printf("ErrorCode:%s",errcode);}voiderror_exit(constchar*errstr,interrcode){error(errstr,errcode);exit(errcode);}voidmain(){ret_code=WSAStartup(MAKEWORD(2,2),&wsaData);if(ret_code!=0){error_exit("WSAStartuperror",ret_code);}//´´½¨Ì×½Ó×Öif((tcpServerSoc
4、ket=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET)error_exit("socketerror",WSAGetLastError());//°ó¶¨IPºÍ¶Ë¿ÚtcpServerAddr.sin_family=AF_INET;tcpServerAddr.sin_port=htons(PORT);tcpServerAddr.sin_addr.S_un.S_addr=inet_addr(IPSTRING);if((bind(tcpServerSocket,(structsockaddr*
5、)&tcpServerAddr,sizeof(tcpServerAddr)))==SOCKET_ERROR)error_exit("binderror",WSAGetLastError());if((listen(tcpServerSocket,backlog))==SOCKET_ERROR)error_exit("listenerror",WSAGetLastError());while(true){SOCKETtcpClientSocket=0;SOCKADDR_INtcpClientAddr={0};inttcpClientAddrL
6、en=sizeof(SOCKADDR_IN);if((tcpClientSocket=accept(tcpServerSocket,(structsockaddr*)&tcpClientAddr,&tcpClientAddrLen))==INVALID_SOCKET){error_exit("accepterror",WSAGetLastError());continue;}printf("FromIP=%sPort=%d",inet_ntoa(tcpClientAddr.sin_addr),ntohs(tcpClientAddr.si
7、n_port));//·¢ËͺͽÓÊÕÊý¾Ý//recv//sendclosesocket(tcpClientSocket);}}#include#include#include#include#pragmacomment(lib,"ws2_32.lib")SOCKETtcpServerSocket=0;SOCKADDR_INtcpServerAddr={0};constintbacklog=SOMAXCONN;WSADATAwsaData={0};i
8、ntret_code;#definePORT7456//serverport#defineIPSTRING"122.*.*.*"//serverip//³ö´í´¦Àíº¯Êýv
此文档下载收益归作者所有