实验报告实验名称--SOCKET编程一、实验目的(1)加深对TCP和UDP的理解;(2)实现两台计算机之间TCP/UDP通信。二、实验过程原理:socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用以实现进程在网络中通信。如下图所示:TCP通信原理如图:代码:服务器端:#pragmacomment(lib,WS2_32.lib)#includeWinsock2.h#includeiostream#includestdlib.husingnamespacestd;intmain(){inti=0;WSADATAwsaData;SOCKEToldSocket,newSocket;//客户地址长度intiLen=0;//发送的数据长度intiSend=0;//接收的数据长度intircv=0;//处世要发送给客户的信息charbuf[20]=Iamaserver;//接收来自用户的信息charfromcli[512];//客户和服务器的SOCKET地址结构structsockaddr_inser,cli;if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0){coutfailedtoloadwinsockendl;return0;}coutserverwaitingendl;cout---------------endl;//创建服务器端帧听SOCKEToldSocket=socket(AF_INET,SOCK_STREAM,0);if(oldSocket==INVALID_SOCKET){coutsocket()failed:WSAGetLastError()endl;return0;}//以下是建立服务器端的SOCKET地址结构ser.sin_family=AF_INET;ser.sin_port=htons(5050);//使用系统指定的ip地址INADDR_ANY//ser.sin_addr.s_addr=htonl(INADDR_ANY);ser.sin_addr.s_addr=inet_addr(192.168.8.58);if(bind(oldSocket,(LPSOCKADDR)&ser,sizeof(ser))==SOCKET_ERROR){coutbind()failed:WSAGetLastError()endl;return0;}//进入侦听状态if(listen(oldSocket,5)==SOCKET_ERROR){coutlisten()failed:WSAGetLastError()endl;return0;}//接收客户端的连接iLen=sizeof(cli);newSocket=accept(oldSocket,(structsockaddr*)&cli,&iLen);//产生一个新的SOCKETif(newSocket==INVALID_SOCKET){coutaccept()failed:WSAGetLastError()endl;//return0;}//进入一个无限循环,等待客户发送数据while(1){i++;//服务器初始化接收缓冲区memset(fromcli,0,512);ircv=recv(newSocket,fromcli,sizeof(fromcli),0);if(ircv==SOCKET_ERROR){coutrcv()failed:WSAGetLastError()endl;break;}elseif(ircv==0)break;else{cout-----服务器接收的内容为--------fromcliendl;}if(strcmp(fromcli,quit)==0)break;}closesocket(newSocket);closesocket(oldSocket);WSACleanup();}客户端:#pragmacomment(lib,WS2_32.lib)#includewinsock2.h#includeiostream#includestdio.husingnamespacestd;#defineDATA_BUFFER512intmain(){WSADATAwsaData;SOCKETsClient;charch;inti=0;char*Serip=192.168.8.58;//随着程序所在主机的地址改变而改变intSeriPort=5050;//从服务器接收的数据的长度intiLen;//发送的数据长度intisnd;//接收缓冲区charbuf[DATA_BUFFER];//发送缓冲区charsndbuf[DATA_BUFFER];//服务器端SOCKET地址结构structsockaddr_inser;//接收/发送缓冲区的初始化memset(buf,0,sizeof(buf));memset(sndbuf,0,DATA_BUFFER);if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0){coutfailedtoloadwinsockendl;return0;}//填写要连接的服务器地址信息ser.sin_family=AF_INET;ser.sin_port=htons(SeriPort);ser.sin_addr.s_addr=inet_addr(Serip);//建立客户端字节流式套接字sClient=socket(AF_INET,SOCK_STREAM,0);if(sClient==INVALID_SOCKET){coutsocket()failed:WSAGetLastError()endl;return0;}//请求与服务器建立连接//coutsetlink;if(connect(sClient,(structsockaddr*)&ser,sizeof(ser))==INVALID_SOCKET){coutconnect()failed:WSAGetLastError()endl;return0;}else{//向服务器端发送数据while(1){coutendl;i++;cout第i发送endl;cinsndbuf;isnd=send(sClient,sndbuf,sizeof(sndbuf),0);if(isnd==0)return0;elseif(isnd==SOCKET_ERROR){coutsend()failed:WSAGetLastError()endl;return0;}cout-----客户端发送内容为----sndbufendl;}}closesocket(sClient);WSACleanup();}本机上客户端与服务器端通信结果如图:在两台服务器上分别运行服务器端和客户端,结果如图:UDP通信原理如图:代码:服务器端:#includestdafx.h#includewinsock2.h#includestdio.h#includestdlib.h#definebuffer_length512#pragmacomment(lib,WS2_32.lib)voidmain(){WSADATAwsadata;SOCKETsSocket;intilen;intirecv;charrecv_buf[buffer_length];charsend_buf[buffer_length];//服务器和客户SOCKET地址结构structsockaddr_inseradd,cliadd;if(WSAStartup(MAKEWORD(2,2),&wsadata)!=0){printf(failedtoloadwinsocket\n);return;}sSocket=socket(AF_INET,SOCK_DGRAM,0);if(sSocket==INVALID_SOCKET){printf(socket()failed:%d\n,WSAGetLastError());return;}//初始化服务器SOCKET地址结构seradd.sin_family=AF_INET;seradd.sin_port=htons(6666);//seradd.sin_addr.s_addr=htonl(INADDR_ANY);seradd.sin_addr.s_addr=inet_addr(0.0.0.0);if(bind(sSocket,(LPSOCKADDR)&seradd,sizeof(seradd))==SOCKET_ERROR){printf(地址绑定时出错:%d\n,WSAGetLastError());intrua;scanf(%d,&rua);return;}ilen=sizeof(cliadd);//初始化接收缓冲区memset(recv_buf,0,sizeof(recv_buf));irecv=recvfrom(sSocket,recv_buf,buffer_length,0,(structsockaddr*)&cliadd,&ilen);if(irecv==SOCKET_ERROR){printf(接收出错%d\n,WSAGetLastError());intrua;scanf(%d,&rua);return;}elseif(irecv==0)return;else{printf(\n%s--,recv_buf);printf(ServerreceivedfromClientip:[%s],port:[%d]\n,inet_ntoa(cliadd.sin_addr),ntohs(cliadd.sin_port));}while(1){//在此函数中cli为传出参数irecv=recvfrom(sSocket,recv_buf,buffer_length,0,(structsockaddr*)&cliadd,&ilen);if(irecv==SOCKET_ERROR){printf(接收出错%d\n,WSAGetLastError());intrua;scanf(%d,&rua);return;}elseif(irecv==0)break;else{printf(\n%s--,recv_buf);printf(ServerreceivedfromClientip:[%s],port:[%d]\n,inet_ntoa(cliadd.sin_addr),ntohs(cliadd.sin_port));}scanf(%s,send_buf);sendto(sSocket,send_buf,sizeof(send_buf),0,(structsockaddr*)&cliadd,sizeof(cliadd));}closesocket(sSocket);WSACleanup();}客户端:#includestdafx.h#includewinsock2.h#includestdio.h#definedata_buffer1024voidmain(){WSADATAwsadata;SOCKETsclient;//服务器地址、端口号char*serip=192.168.8.58;intSeriport=6666;charch[]=qita;//服务器SOCKET地址结构长度intilen;//发送/接收数据大小intisend;intirecv;//要发送给服务器的数据charsend_buf[]=hello,Iamaclient.;charrecv_buf[data_buffer];//服务器端的SOCKET地址结构structsockaddr_inseradd,cliadd;//初始化接收缓冲