TCPIP协议实验报告一实验名称:基于TCP的客户、服务器通信程序组员:潘誉0304110428谢陈洁0304110430实验时间:2014-3-11二实验目的:通过此次实例对网络程序的基本框架有个比较清楚的认识,为后面设计比较复杂的网络程序打好基础。三实验内容本程序使用了visualc++6.0下的控制台程序“win32consoleapplication”。控制台程序结构一目了然。该程序中通信协议使用的是面向连接的TCP协议,服务端的IP地址使用系统指定的IP地址,端口号在程序中指定为5050,用符号常量来定义。服务器端的功能是只要有客户请求连接,服务器就接受连接,在连接成功后,服务器就向请求连接的客户端发送内容,如果发送过程正确完成,则显示所发送的字节数。客户端功能是向服务器发出连接请求,在连接成功后,接受并显示从服务端收到的信息。四程序清单服务器端代码//TcpServer.cpp:Definestheentrypointfortheconsoleapplication.//#includestdafx.h#includeWinsock2.h#includestdio.h#includestdlib.h#defineDEFAULT_PORT5050#defineBUF_SIZE1024voidmain(intargc,char*argv[]){//printf(HelloWorld!\n);//return0;intiPort=DEFAULT_PORT;WSADATAwsaData;SOCKETsListen,sAccept;intiLen;intiSend;charbuf[]=Iamaserver.;structsockaddr_inser,cli;charreceiver[BUF_SIZE];charusername[BUF_SIZE];memset(receiver,0,sizeof(receiver));memset(username,0,sizeof(username));printf(-----------------------\n);printf(Serverwaiting\n);printf(-----------------------\n);if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0){printf(FailedtoloadWinsock.\n);return;}sListen=socket(AF_INET,SOCK_STREAM,0);if(sListen==INVALID_SOCKET){printf(socket()Failed:%d\n,WSAGetLastError());return;}ser.sin_family=AF_INET;ser.sin_port=htons(iPort);ser.sin_addr.s_addr=htonl(INADDR_ANY);if(bind(sListen,(LPSOCKADDR)&ser,sizeof(ser))==SOCKET_ERROR){printf(bind()Failed:%d\n,WSAGetLastError());return;}if(listen(sListen,5)==SOCKET_ERROR){printf(listen()Failed:%d\n,WSAGetLastError());return;}iLen=sizeof(cli);while(1){sAccept=accept(sListen,(structsockaddr*)&cli,&iLen);if(sAccept==INVALID_SOCKET){printf(accept()Failed:%d\n,WSAGetLastError());break;}printf(AcceptedclientIP:[%s],port:[%d]\n,inet_ntoa(cli.sin_addr),ntohs(cli.sin_port));iSend=send(sAccept,buf,sizeof(buf),0);if(iSend==SOCKET_ERROR){printf(send()Failed:%d\n,WSAGetLastError());break;}elseif(iSend==0){break;}else{printf(send()byte:%d\n,iSend);printf(--------------------\n);}iLen=recv(sAccept,username,sizeof(username),0);if(iLen==0){return;}elseif(iLen==SOCKET_ERROR){printf(recv()Failed:%d\n,WSAGetLastError());return;}printf(recv()datafromclient:%s\n,receiver);while(1){iLen=recv(sAccept,receiver,sizeof(receiver),0);if(iLen==0){return;}elseif(iLen==SOCKET_ERROR){printf(recv()Failed:%d\n,WSAGetLastError());return;}printf(%s:%s\n,username,receiver);}closesocket(sAccept);}closesocket(sListen);WSACleanup();}客户端代码://TcpClient.cpp:Definestheentrypointfortheconsoleapplication.//#includestdafx.h#includeWinsock2.h#includestdio.h#defineDEFAULT_PORT5050#defineBUF_SIZE1024voidmain(intargc,char*argv[]){WSADATAwsaData;SOCKETsClient;intiPort=DEFAULT_PORT;intiLen;intiSend;charbuf[BUF_SIZE];structsockaddr_inser;charword[BUF_SIZE];if(argc2){printf(Usage:client[serverIPaddress]\n);return;}memset(buf,0,sizeof(buf));if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0){printf(FailedtoloadWinsock.\n);return;}ser.sin_family=AF_INET;ser.sin_port=htons(iPort);ser.sin_addr.s_addr=inet_addr(argv[1]);//while(getchar()!=EOF){sClient=socket(AF_INET,SOCK_STREAM,0);if(sClient==INVALID_SOCKET){printf(socket()Failed:%d\n,WSAGetLastError());return;}if(connect(sClient,(structsockaddr*)&ser,sizeof(ser))==INVALID_SOCKET){printf(connect()Failed:%d\n,WSAGetLastError());return;}else{iLen=recv(sClient,buf,sizeof(buf),0);if(iLen==0){return;}elseif(iLen==SOCKET_ERROR){printf(recv()Failed:%d\n,WSAGetLastError());return;}printf(recv()datafromserver:%s\n,buf);printf(Pleaseenteryourname:);//scanf(%s,&word);//gets(word);//while(getchar()!=EOF){gets(word);iSend=send(sClient,word,sizeof(word),0);if(iSend==SOCKET_ERROR){printf(send()Failed:%d\n,WSAGetLastError());return;}elseif(send==0){return;}else{printf(SendOK.\n);printf(------------------------\n);}while(gets(word)){iSend=send(sClient,word,sizeof(word),0);if(iSend==SOCKET_ERROR){printf(send()Failed:%d\n,WSAGetLastError());return;}elseif(send==0){break;}else{printf(SendOK.\n);printf(------------------------\n);}}}closesocket(sClient);WSACleanup();}五实验结果:客户端连接成功,发送消息接收服务器端消息服务器端发送消息客户端接收消息