您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 其它文档 > 计算机网络课程实验报告
1第一章实验一1.1实验内容和要求进行网络编程,实现客户端和服务器端的通信。1.2实验思路利用windows套接字,在vs控制台环境下进行编程,分别编写客户端与服务器端。通信时先打开服务器端,等待连接,再打开客户端,连接成功后按提示输入数据,回车后在服务器端显示;根据服务器端提示输入数据,回车后在客户端显示,实现双向通信。1.3运行结果先运行服务器端,再打开客户端,出现如下提示,然后输入数据回车后,在服务器端接受到数据“1234abcd”,并要求继续输入2输入“345abre”31.4程序源代码客户端源码intmain(intargc,char*argv[]){constintBUF_SIZE=128;WSADATAwsd;//WSADATA变量SOCKETsHost;//服务端套接字SOCKADDR_INservAddr;//服务端地址charbuf[BUF_SIZE];//接收数据缓冲区charbufRecv[BUF_SIZE];intretVal;//返回值cout此客户端通过TCP向服务端10.10.14.121发送消息\n;coutendl;//初始化套接字动态库if(WSAStartup(MAKEWORD(2,2),&wsd)!=0){coutWSAStartupfailed!endl;return-1;}//创建套接字sHost=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(INVALID_SOCKET==sHost){coutsocketfailed!endl;WSACleanup();//释放套接字资源return-1;}//设置服务端地址servAddr.sin_family=AF_INET;//指定使用TCP/IPservAddr.sin_addr.s_addr=inet_addr(10.10.14.121);//指定服务端IP地址servAddr.sin_port=htons((short)1032);//指定服务端端口intnServAddlen=sizeof(servAddr);//连接服务端retVal=connect(sHost,(LPSOCKADDR)&servAddr,sizeof(servAddr));if(SOCKET_ERROR==retVal){coutconnectfailed!endl;4closesocket(sHost);//关闭套接字WSACleanup();//释放套接字资源return-1;}while(true){//向服务端发送数据ZeroMemory(buf,BUF_SIZE);coutClient:;cinbuf;retVal=send(sHost,buf,strlen(buf),0);if(SOCKET_ERROR==retVal){coutsendfailed!endl;closesocket(sHost);//关闭套接字WSACleanup();//释放套接字资源return-1;}//接收服务端的数据ZeroMemory(bufRecv,BUF_SIZE);recv(sHost,bufRecv,BUF_SIZE,0);coutSever:bufRecv;cout\n;}//退出closesocket(sHost);//关闭套接字WSACleanup();//释放套接字资源return0;}服务器端源码intmain(intargc,char*argv[]){constintBUF_SIZE=128;WSADATAwsd;//WSADATA变量SOCKETsServer;//服务端套接字SOCKETsClient;//客户端套接字SOCKADDR_INaddrServ;//服务端地址charbuf[BUF_SIZE];//接收数据缓冲区charsendBuf[BUF_SIZE];//返回给客户端的数据5intretVal;//返回值coutendl;//初始化套接字动态库if(WSAStartup(MAKEWORD(2,2),&wsd)!=0){coutWSAStartupfailed!endl;return1;}//创建套接字sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(INVALID_SOCKET==sServer){coutsocketfailed!endl;WSACleanup();//释放套接字资源;return-1;}//服务器套接字地址addrServ.sin_family=AF_INET;//指定使用TCP/IP协议addrServ.sin_port=htons(1032);//设置端口号addrServ.sin_addr.s_addr=inet_addr(10.10.14.121);//设置服务端IP地址//绑定套接字retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));if(SOCKET_ERROR==retVal){coutbindfailed!endl;closesocket(sServer);//关闭套接字WSACleanup();//释放套接字资源;return-1;}//开始监听retVal=listen(sServer,1);if(SOCKET_ERROR==retVal){coutlistenfailed!endl;closesocket(sServer);//关闭套接字WSACleanup();//释放套接字资源;return-1;}6//接受客户端请求sockaddr_inaddrClient;intaddrClientlen=sizeof(addrClient);sClient=accept(sServer,(sockaddrFAR*)&addrClient,&addrClientlen);if(INVALID_SOCKET==sClient){coutacceptfailed!endl;closesocket(sServer);//关闭套接字WSACleanup();//释放套接字资源;return-1;}while(true){//接收客户端数据ZeroMemory(buf,BUF_SIZE);retVal=recv(sClient,buf,BUF_SIZE,0);if(SOCKET_ERROR==retVal){cout客户端已关闭,通话结束!endl;coutendl;system(pause);closesocket(sServer);//关闭套接字closesocket(sClient);//关闭套接字WSACleanup();//释放套接字资源;return-1;}if(buf[0]=='\0')break;coutClient:bufendl;//给客户端发送数据coutSever:;cinsendBuf;send(sClient,sendBuf,strlen(sendBuf),0);}//退出closesocket(sServer);//关闭套接字closesocket(sClient);//关闭套接字WSACleanup();//释放套接字资源;return0;}7第二章实验二2.1实验内容与要求实现TCP通信。2.2实验思路先用python实现TCP通信,再将TCP协议的头加上数据转化为二进制传给用户,再解码得到所需数据。2.3运行结果2.4源代码importsocketip_port=('127.0.0.1',8888)sk=socket.socket()sk.connect(ip_port)sk.settimeout(5)whileTrue:data=sk.recv(1024)data=data.decode()print('receive:',str(data))inp=input('pleaseinput:')sk.sendall(bytes(inp,encoding=utf-8))ifinp=='exit':breaksk.close()8服务器端importsocketip_port=('127.0.0.1',8888)sk=socket.socket()sk.bind(ip_port)sk.listen(5)whileTrue:conn,address=sk.accept()conn.send('请输入0:'.encode('utf-8'))Flag=TruewhileFlag:data=conn.recv(1024)ifdata=='exit':Flag=Falseelifdata=='0':conn.sendall('连接成功'.encode())elifdata=='zero':conn.sendall(bytes('连接成功',encoding=utf-8))else:data=连接成功:+str(data.decode())+\n请重新输入:conn.sendall(bytes(data,encoding=utf-8))conn.close()20字节的TCP头部Classtcpheader:Sport=0Dport=0Seq=0Ack=0H_length=‘’9Flag=‘’Wsize=0Cksum=0Urrgpoint=0Options[12]=[]对options进行赋初值tempdata[12]={0x02,0x04,0x05,0xb4,0x01,0x03,0x03,0x02,0x01,0x01,0x04,0x02}memcpy(tcpHeader.options,tempdata,12)合并数据包Buff=tcpHeader+shuju
本文标题:计算机网络课程实验报告
链接地址:https://www.777doc.com/doc-5221093 .html