华科计算机网络实验报告(共13页).docx
《华科计算机网络实验报告(共13页).docx》由会员分享,可在线阅读,更多相关《华科计算机网络实验报告(共13页).docx(13页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上电子与信息工程系实 验 报 告实验名称Windows Socket 编程(Duplex-talk)课程名称计算机网络姓名张建佳学号U日期2012-03-21地点南一楼东203成绩教师徐晶专心-专注-专业1. 实验目的1掌握网络应用程序的开发方法;2掌握Client/ Server 结构软件的设计与开发方法3掌握Socket 机制的工作原理2. 实验环境操作系统:Windows 7开发环境:Visual Studio 2010,VC63. 实验内容与结果2. 修改simplex-talk 程序,编制duplex-talk 程序,支持client 和server 的双向通
2、信(1)程序整体功能Client端程序连接Server端程序,Client能给Server发送信息,同时Server也能给Client发送信息,实现双向通信;(2) 程序组成及各模块/函数功能Client端传入server端的主机号将server端的主机号转换为IP地址发起一个socket连接请求判断连接是否成功N报错,退出Y发送信息判断信息是否为空YNN判断信息是否为空接收信息Y关闭连接退出程序Server监听等到Client的连接请求建立与Client的连接接收信息N判断信息是否为空发送信息YN判断信息是否为空Y关闭连接退出程序(3)程序清单(见附件)(4)程序的运行和测试结果编译源文件,
3、生成可执行文件并运行,其过程如下:Client端Socket端server.exesimplex-talk server server is ready in listening .client.exe localhostsimplex-talk server received a connection from 127.0.0.1 :simplex-talk client connection to localhost is readysimplex-talk client please input your message (empty input to halt):Hello,Serve
4、r.simplex-talk client send 14 chars to serversimplex-talk server received 14 charsHello,Server.Hello,Client.simplex-talk server send 14 chars to clientsimplex-talk client received 14 charsHello,Client.Bye.simplex-talk client send 5 chars to serversimplex-talk server received 5 charsBye.simplex-talk
5、server empty message is send to clientsimplex-talk server connection from 127.0.0.1 is terminatedsimplex-talk client empty message is receivedsimplex-talk client connection is terminated运行结果截图ClientServer4. 实验中的问题暂无附件1.程序源代码(1)socket-client.c键入文档的引述或关注点的摘要。您可将文本框放置在文档中的任何位置。请使用“绘图工具”选项卡更改引言文本框的格式。/*
6、 wliu comments: required for windows socket programming */#include #pragma comment(lib, wsock32.lib) #include #include #define SERVER_PORT 5432#define MAX_BUFSIZE 256int main(int argc, char * argv) /* wliu comments: required for windows socket programming */ WSADATA WSAData; int WSAreturn;/* wliu co
7、mments: useless pointer */ /FILE *fp; struct hostent *hp; struct sockaddr_in sin; char *host; char bufInMAX_BUFSIZE,bufOutMAX_BUFSIZE; int s, new_s; int len;int ableToSendMsg = 1;/Lim Wug comments: This variable acts as a flag to send and receive messagehost = localhost; / if (argc=2) / host = argv1
8、; / / else / fprintf(stderr, usage: simplex-talk hostn);/ exit(1); / /* wliu comments: modified for windows socket programming */WSAreturn = WSAStartup(0x101,&WSAData);if(WSAreturn)fprintf(stderr, simplex-talk: WSA error.n);exit(1); /* translate host name into peers IP address */ hp = gethostbyname(
9、host);if (!hp) fprintf(stderr, simplex-talk: unknown host: %sn, host);exit(1); /* wliu comments: modified for string memory operation in windows */ /bzero(char *)&sin, sizeof(sin); /bcopy(hp-h_addr, (char *)&sin.sin_addr, hp-h_length); /* build address data structure */ memset(char *)&sin, 0, sizeof
10、(sin); memcpy(char *)&sin.sin_addr, hp-h_addr, hp-h_length); sin.sin_family = AF_INET; sin.sin_port = htons(SERVER_PORT); /* active open */ if (s = socket(PF_INET, SOCK_STREAM, 0) 0) perror(simplex-talk: socket failed.);exit(1); if (connect(s, (struct sockaddr *)&sin, sizeof(sin) 0) perror(simplex-t
11、alk: connect failed.);/* wliu comments: modified for windows socket programming */close(s);closesocket(s);exit(1);/* wliu comments: displaying current status */ printf(simplex-talk client connection to %s is readyn, host);printf(simplex-talk client please input your message (empty input to halt):n);
12、 /* wliu comments: modification to support connection termination */ /while (fgets(buf, sizeof(buf), stdin) /* main loop: get and send lines of text */ while (1) if(ableToSendMsg)/Lim Wug comments:Send Messgagefgets(bufIn, sizeof(bufIn), stdin);/* wliu comments: modified to stop sending */ if ( strl
13、en(bufIn) = 1 ) /* wliu comments: user input empty message with n */bufIn0 = 0;send(s, bufIn, 1, 0);printf(simplex-talk client empty message is send to servern);break; else bufInMAX_BUFSIZE-1 = 0;len = strlen(bufIn) + 1;send(s, bufIn, len, 0);printf(simplex-talk client send %d chars to servernn, str
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机网络 实验 报告 13
限制150内