您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 其它文档 > 华科-计算机网络-实验报告1
-1-电子与信息工程系实验报告实验名称WindowsSocket编程(Duplex-talk)课程名称计算机网络姓名张建佳学号U201013086日期2012-03-21地点南一楼东203成绩教师徐晶-1-1.实验目的1.掌握网络应用程序的开发方法;2.掌握Client/Server结构软件的设计与开发方法3.掌握Socket机制的工作原理2.实验环境操作系统:Windows7开发环境:VisualStudio2010,VC63.实验内容与结果2.修改simplex-talk程序,编制duplex-talk程序,支持client和server的双向通信(1)程序整体功能Client端程序连接Server端程序,Client能给Server发送信息,同时Server也能给Client发送信息,实现双向通信;(2)程序组成及各模块/函数功能Client端传入server端的主机号将server端的主机号转换为IP地址发起一个socket连接请求判断连接是否成功报错,退出N华中科技大学电子与信息工程系实验报告-2-Server发送信息关闭连接接收信息判断信息是否为空YNY监听等到Client的连接请求建立与Client的连接判断信息是否为空NY退出程序华中科技大学电子与信息工程系实验报告-3-(3)程序清单(见附件)(4)程序的运行和测试结果编译源文件,生成可执行文件并运行,其过程如下:Client端Socket端server.exe[simplex-talkserver]serverisreadyinlistening...client.exelocalhost[simplex-talkserver]receivedaconnectionfrom127.0.0.1:[simplex-talkclient]connectiontolocalhostisready[simplex-talkclient]pleaseinputyourmessage(emptyinputtohalt):Hello,Server.[simplex-talkclient]send14charstoserver[simplex-talkserver]received14charsHello,Server.接收信息关闭连接发送信息判断信息是否为空NN判断信息是否为空YY退出程序华中科技大学电子与信息工程系实验报告-4-Hello,Client.[simplex-talkserver]send14charstoclient[simplex-talkclient]received14charsHello,Client.Bye.[simplex-talkclient]send5charstoserver[simplex-talkserver]received5charsBye.[simplex-talkserver]emptymessageissendtoclient[simplex-talkserver]connectionfrom127.0.0.1isterminated[simplex-talkclient]emptymessageisreceived[simplex-talkclient]connectionisterminated运行结果截图ClientServer华中科技大学电子与信息工程系实验报告-5-4.实验中的问题暂无附件1.程序源代码(1)socket-client.c[键入文档的引述或关注点的摘要。您可将文本框放置在文档中的任何位置。请使用“绘图工具”选项卡更改引言文本框的格式。]华中科技大学电子与信息工程系实验报告-6-/*wliucomments:requiredforwindowssocketprogramming*/#includewinsock.h#pragmacomment(lib,wsock32.lib)#includestdio.h#includestring.h#defineSERVER_PORT5432#defineMAX_BUFSIZE256intmain(intargc,char*argv[]){/*wliucomments:requiredforwindowssocketprogramming*/WSADATAWSAData;intWSAreturn;/*wliucomments:uselesspointer*///FILE*fp;structhostent*hp;structsockaddr_insin;char*host;charbufIn[MAX_BUFSIZE],bufOut[MAX_BUFSIZE];ints,new_s;intlen;intableToSendMsg=1;//LimWugcomments:Thisvariableactsasaflagtosendandreceivemessagehost=localhost;//if(argc==2){//host=argv[1];//}//else{//fprintf(stderr,usage:simplex-talkhost\n);//exit(1);//}/*wliucomments:modifiedforwindowssocketprogramming*/WSAreturn=WSAStartup(0x101,&WSAData);if(WSAreturn){fprintf(stderr,simplex-talk:WSAerror.\n);exit(1);}/*translatehostnameintopeer'sIPaddress*/hp=gethostbyname(host);华中科技大学电子与信息工程系实验报告-7-if(!hp){fprintf(stderr,simplex-talk:unknownhost:%s\n,host);exit(1);}/*wliucomments:modifiedforstringmemoryoperationinwindows*///bzero((char*)&sin,sizeof(sin));//bcopy(hp-h_addr,(char*)&sin.sin_addr,hp-h_length);/*buildaddressdatastructure*/memset((char*)&sin,0,sizeof(sin));memcpy((char*)&sin.sin_addr,hp-h_addr,hp-h_length);sin.sin_family=AF_INET;sin.sin_port=htons(SERVER_PORT);/*activeopen*/if((s=socket(PF_INET,SOCK_STREAM,0))0){perror(simplex-talk:socketfailed.);exit(1);}if(connect(s,(structsockaddr*)&sin,sizeof(sin))0){perror(simplex-talk:connectfailed.);/*wliucomments:modifiedforwindowssocketprogramming*///close(s);closesocket(s);exit(1);}/*wliucomments:displayingcurrentstatus*/printf([simplex-talkclient]connectionto%sisready\n,host);printf([simplex-talkclient]pleaseinputyourmessage(emptyinputtohalt):\n);/*wliucomments:modificationtosupportconnectiontermination*///while(fgets(buf,sizeof(buf),stdin)){/*mainloop:getandsendlinesoftext*/while(1){if(ableToSendMsg){//LimWugcomments:SendMessgagefgets(bufIn,sizeof(bufIn),stdin);/*wliucomments:modifiedtostopsending*/if(strlen(bufIn)==1){华中科技大学电子与信息工程系实验报告-8-/*wliucomments:userinputemptymessagewith'\n'*/bufIn[0]='\0';send(s,bufIn,1,0);printf([simplex-talkclient]emptymessageissendtoserver\n);break;}else{bufIn[MAX_BUFSIZE-1]='\0';len=strlen(bufIn)+1;send(s,bufIn,len,0);printf([simplex-talkclient]send%dcharstoserver\n\n,strlen(bufIn));ableToSendMsg=0;}}else{//LimWugcomments:ReceiveMessagelen=recv(s,bufOut,sizeof(bufOut),0);///*wliucomments:modifiedtostopsending*/if(strlen(bufOut)==0){/*wliucomments:receivedemptymessage*/printf([simplex-talkclient]emptymessageisreceived\n);break;}else{printf([simplex-talkclient]received%dchars\n,strlen(bufOut));fputs(bufOut,stdout);printf(\n);ableToSendMsg=1;}}}printf([simplex-talkclient]connectionisterminated\n);/*wliucomments:modifiedforwindowssocketprogramming*/WSACleanup();return1;}华中科技大学电子与信息工程系实验报告-9-Socket-server.c/*---------------------------------------------------*///filename:socket-server.c//fileuseage:demostratethebasicTCP-basedblocked//socketprogramminginwindowssystem//fileorigin:demoofsimplex-talk//computernetworks,systemapproach,4th//modifiedbyWeiLiu,06/12/2011//modifiedbyLimWug,03/21/2012/*---------------------------------------------------*//*wliucomments:requiredforlinuxsocketprogramming*///#includesys/types.h//#includesys/socket.h//#includenetinet/in.h//#includenetdb.h/*wliucomments:requiredforwindowssocketprogramming*/#includewinsock.h#pragmacomment(lib,wsock32.lib)#includestdio.h#includestring.h#defineSERVER_PORT5432#defineMAX_PENDING5#defineMA
本文标题:华科-计算机网络-实验报告1
链接地址:https://www.777doc.com/doc-6949781 .html