您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 12计算机1-实验08--TCP-IP-Socket编程
《计算机网络》实验报告实验序号:8实验项目名称:TCP_IPSocket编程学号1207012116姓名黄进福专业、班12嵌入式1班实验地点实1-518指导教师李桂森实验时间2014/12/30一、实验目的及要求1、掌握抓包软件Wireshark的使用2、进一步掌握ARP、ICMP、HTTP等常见协议的基本原理。二、实验设备(环境)及要求环境:WindowsXP,Wireshark三、实验内容与步骤(1)分别编写基于TCP的socket程序服务端和客户端,要求客户端能发送消息到服务端,服务端再把此消息返回给客户端。(2)在上述程序的基础上,实现客户端和服务端间的简易聊天功能,即服务端和客户端能依次从键盘输入文字信息并发送(要求在两台不同的电脑上进行)。(3)改用UDP实现此程序的功能。(4)使用netstat命令观察程序运行前后的端口变化情况并记录下来。四、实验心得体会五、教师评语成绩签名:日期:TCP源码:服务端://#includestdafx.h#includestdio.h#includewinsock2.h#pragmacomment(lib,WS2_32.lib)#defineBUF_SIZE1024intmain(intargc,char*argv[]){WSADATAwsaData;SOCKETsocketL;//socketforlisteningSOCKETsocketC;//socketforCommunicationSOCKADDR_INserverAddr;if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)//建立版本{printf(WSAStartupfailed!\n);return1;}socketL=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//设置为TCP流模式serverAddr.sin_family=AF_INET;serverAddr.sin_port=htons(5678);//侦听端口为5678serverAddr.sin_addr.s_addr=htonl(INADDR_ANY);//INADDR_ANY为本机任意IPbind(socketL,(SOCKADDR*)&serverAddr,sizeof(serverAddr));//绑定到SOCKETlisten(socketL,5);//侦听sockSrv,并设置最大客户连接数为5。SOCKADDR_INaddrClient;intlen=sizeof(SOCKADDR);socketC=accept(socketL,(SOCKADDR*)&addrClient,&len);//等待客户连接,如果没有客户连接时,则此函数在这里会产生阻塞。if(socketC==INVALID_SOCKET){return1;}intnRet=-1;charrecvbuf[BUF_SIZE];chartempBuf[BUF_SIZE];charsendBuf[BUF_SIZE];while(1){nRet=recv(socketC,recvbuf,sizeof(recvbuf),0);//从客户端接收。if(nRet0){if('q'==recvbuf[0]){send(socketC,q,strlen(q)+1,0);//发送q给客户端。printf(Chatend!\n);break;}sprintf(tempBuf,%ssay:%s,inet_ntoa(addrClient.sin_addr),recvbuf);printf(%s\n,tempBuf);//显示客户端发过来的字符串printf(Pleaseinputdata:\n);gets(sendBuf);send(socketC,sendBuf,sizeof(sendBuf),0);}}closesocket(socketC);closesocket(socketL);WSACleanup();return0;}客户端://#includestdafx.h#includestdio.h#includewinsock2.h#pragmacomment(lib,WS2_32.lib)#defineBUF_SIZE1024intmain(intargc,char*argv[]){WSADATAwsaData;SOCKADDR_INserverAddr;SOCKETsocketC;if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0){printf(WSAStartupfailed!\n);return1;}socketC=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);serverAddr.sin_family=AF_INET;serverAddr.sin_port=htons(5678);serverAddr.sin_addr.s_addr=inet_addr(172.16.192.187);connect(socketC,(SOCKADDR*)&serverAddr,sizeof(serverAddr));charrecvbuf[BUF_SIZE];chartempBuf[BUF_SIZE];charsendBuf[BUF_SIZE];while(1){printf(Pleaseinputdata:\n);gets(sendBuf);send(socketC,sendBuf,sizeof(sendBuf),0);intnRet=recv(socketC,recvbuf,sizeof(recvbuf),0);//从服务端接收。if(nRet0){if('q'==recvbuf[0]){send(socketC,q,strlen(q)+1,0);//发送q给服务端。printf(Chatend!\n);break;}sprintf(tempBuf,%ssay:%s,inet_ntoa(serverAddr.sin_addr),recvbuf);printf(%s\n,tempBuf);//显示服务端发过来的字符串}}closesocket(socketC);WSACleanup();return0;}UDP源码:服务端:#includestdio.h#includewinsock2.h//WINSOCK.H(这个是WINSOCKAPI的头文件,WIN2K以上支持WINSOCK2,所以//可以用WINSOCK2.H);#pragmacomment(lib,WS2_32.lib)//Ws2_32.lib(WINSOCKAPI连接库文件).#defineBUF_SIZE1024//设定BUF的长度intmain(intargc,char*argv[]){WSADATAwsaData;//SOCKETsocketL;//socketforlistening监听SOCKETsocketC;//socketforCommunication通信//SOCKADDR_INserverAddr;//SOCKET的数据结构包含地址端口IP地址structsockaddr_inlocal;structsockaddr_infrom;intfromlen=sizeof(from);local.sin_family=AF_INET;local.sin_port=htons(5678);///监听端口local.sin_addr.s_addr=inet_addr(172.16.192.191);///本机intnRet=-1;charrecvbuf[BUF_SIZE];charsendbuf[BUF_SIZE];if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0){printf(WSAStartupfailed!\n);return1;}socketL=socket(AF_INET,SOCK_DGRAM,0);//创建tcp协议套接字local.sin_family=AF_INET;//IPV4协议local.sin_port=htons(5678);//选择一个端口local.sin_addr.s_addr=htonl(INADDR_ANY);//本机IP地址bind(socketL,(SOCKADDR*)&local,sizeof(local));//此函数将SCOKET与本机上的一个端口相关联,随后就可以在端口监听服务请求while(1){printf(Recievemessage:);recvfrom(socketL,recvbuf,sizeof(recvbuf),0,(structsockaddr*)&from,&fromlen);printf(%s\n,recvbuf);printf(Sendmessage:);gets(sendbuf);sendto(socketL,sendbuf,sizeof(sendbuf),0,(structsockaddr*)&from,fromlen);}closesocket(socketL);WSACleanup();printf(请按回车键关闭);getchar();return0;}客户端:#includestdio.h#includewinsock2.h#pragmacomment(lib,WS2_32.lib)#defineBUF_SIZE1024intmain(intargc,char*argv[]){WSADATAwsaData;structsockaddr_inserver;//SOCKET的数据结构包含地址端口IP地址intlen=sizeof(server);SOCKETsocketC;//创建套接字CintnRet=-1;charsendbuf[BUF_SIZE];//发送数据BUFcharrecvbuf[BUF_SIZE];if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0){printf(WSAStartupfailed!\n);return1;}socketC=socket(AF_INET,SOCK_DGRAM,0);//创建tcp协议套接字server.sin_family=AF_INET;//IPV4协议server.sin_port=htons(5678);//选择一个端口server.sin_addr.s_addr=inet_addr(172.16.192.191);while(1){printf(pleaseinputmessage:);scanf(%s,sendbuf);sendto(socketC,sendbuf,sizeof(sendbuf),0,(structsockaddr*)&server,len);printf(\n);printf(服务端:);recvfrom(socketC,recvbuf,sizeof(recvbuf),0,(structsockaddr*)&server,&len);printf(%s\n,recvbuf);}closesocket(socketC);WSACleanup();printf(请按回车键关闭);getchar();return0;}
本文标题:12计算机1-实验08--TCP-IP-Socket编程
链接地址:https://www.777doc.com/doc-5795500 .html