第一次实验试验报告_第1页
第一次实验试验报告_第2页
第一次实验试验报告_第3页
第一次实验试验报告_第4页
第一次实验试验报告_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、华中科技大学 电子与信息工程系实验报告- PAGE 19 - PAGE 1 -电子与信息工程系实 验 报 告实验名称Windows Socket 编程课程名称计算机网络姓名张煌泽学号U201013030日期2012-3-27地点南一楼东204成绩教师徐晶老师实验目的1掌握网络应用程序的开发方法;2掌握Client/ Server 结构软件的设计与开发方法3掌握Socket 机制的工作原理实验环境运行的操作系统:windows XP,VC 6.0实验内容与结果软件编程类实验报告内容:1演示单工过程(省略截图,重点讨论双工)2修改完成通信双工过程程序整体功能;实验整体实现将原来只能由client接

2、受到socket传来的数据,改成可以将socket和client两端可以互相发送和接受数据。程序组成及各模块/函数功能;函数功能主体是实现发送和接受双方的相互传送数据的代码主体功能的源代码分为socket-client和socket-server的其中socket-clientt主体功能的源代码为while (1) fgets(buf, sizeof(buf), stdin);/* wliu comments: modified to stop sending */ if ( strlen(buf) = 1 ) /* wliu comments: user input empty messag

3、e with n */buf0 = 0;send(s, buf, 1, 0);printf(simplex-talk client empty message is send to servern);break; else bufMAX_BUFSIZE-1 = 0;len = strlen(buf) + 1;send(s, buf, len, 0);printf(simplex-talk client send %d chars to servern, strlen(buf); printf(simplex-talk client connection is terminatedn); /*

4、wliu comments: modified for windows socket programming */ WSACleanup(); return 1;socket-clientt修改后的主体代码为下面所示 while (1) printf(duplex-talk client client is starting to talk (empty input to halt):n);fgets(buf, sizeof(buf), stdin);/* wliu comments: modified to stop sending */ if ( strlen(buf) = 1 ) /*

5、wliu comments: user input empty message with n */buf0 = 0;send(s, buf, 1, 0);printf(duplex-talk client empty message is send to servern);break; else bufMAX_BUFSIZE-1 = 0;len = strlen(buf) + 1;send(s, buf, len, 0);printf(duplex-talk client send %d chars to servern, strlen(buf);printf(duplex-listen cl

6、ient client is starting to listen. n);/此处为添加部分 len = recv(s, buf, sizeof(buf), 0);if ( strlen(buf) = 0 ) printf(duplex-listen client empty message is receivedn);break; else printf(duplex-listen client received %d chars n, strlen(buf);fputs(buf, stdout);/添加部分结束 printf(duplex-talk client connection is

7、 terminatedn); /* wliu comments: modified for windows socket programming */ WSACleanup(); return 1;socket-server的源代码如下while (1) len = recv(new_s, buf, sizeof(buf), 0);/* wliu comments: modified to stop sending */ if ( strlen(buf) = 0 ) /* wliu comments: received empty message */printf(simplex-talk s

8、erver empty message is receivedn);break; else printf(simplex-talk server received %d chars n, strlen(buf);fputs(buf, stdout); printf(simplex-talk server connection from %s is terminatedn, inet_ntoa(sin.sin_addr);/* wliu comments: modified for windows socket programming */close(new_s);closesocket(new

9、_s);conn +; printf(simplex-talk server max_connections achieved, server haltn); /* wliu comments: required for windows socket programming */ WSACleanup();return 1;修改后的主体代码为下面所示while (1) printf(duplex-listen server server is starting to listen. n); len = recv(new_s, buf, sizeof(buf), 0);/* wliu comme

10、nts: modified to stop sending */ if ( strlen(buf) = 0 ) /* wliu comments: received empty message */printf(duplex-listen server empty message is receivedn);break; else printf(duplex-listen server received %d chars n, strlen(buf);fputs(buf, stdout); printf(duplex-talk server server is starting to talk

11、 (empty input to halt):n); fgets(buf, sizeof(buf), stdin);if ( strlen(buf) = 1 ) buf0 = 0;send(new_s, buf, 1, 0);printf(duplex-talk server empty message is send to clientn);break; else bufMAX_BUFSIZE-1 = 0;len = strlen(buf) + 1;send(new_s, buf, len, 0);printf(duplex-talk server send %d chars to clie

12、ntn, strlen(buf); printf(duplex-talk server connection from %s is terminatedn, inet_ntoa(sin.sin_addr);/* wliu comments: modified for windows socket programming */close(new_s);closesocket(new_s);conn +; printf(duplex-talk server max_connections achieved, server haltn); /* wliu comments: required for

13、 windows socket programming */ WSACleanup();return 1;主要的操作是将在socket-serve中加入 printf(duplex-talk server server is starting to talk (empty input to halt):n); fgets(buf, sizeof(buf), stdin);if ( strlen(buf) = 1 ) buf0 = 0;send(new_s, buf, 1, 0);printf(duplex-talk server empty message is send to clientn

14、);break; else bufMAX_BUFSIZE-1 = 0;len = strlen(buf) + 1;send(new_s, buf, len, 0);printf(duplex-talk server send %d chars to clientn, strlen(buf);在socket-client加入下面代码 len = recv(s, buf, sizeof(buf), 0);if ( strlen(buf) = 0 ) printf(duplex-listen client empty message is receivedn);break; else printf(

15、duplex-listen client received %d chars n, strlen(buf);fputs(buf, stdout);程序整体功能;通过socket编程实现双工通信过程,即client给server发送消息,同时server可以给client回复消息,并能在双方窗口有消息显示。程序组成及各模块/函数功能;程序分为两边1.client:socket:创建“插座”(client名为new_s)使两边可以连接Gethostbyname:根据主机名获取ip地址,便于连接。Connect:向server发送连接socket_s及socket_new_s的请求。Send:发送报

16、文(名为buf)Recv:接收开来自server的报文(名为buf_)Closesocket:关闭连接。2.server:socket:创建“插座”(server名为s)使两边可以连接Bind:把socket绑定到具体地址Listen:监听等待client的接入。Accept:接受connect的连接请求。Send:发送报文(名为buf_)Recv:接收开来自server的报文(名为buf)Closesocket:关闭连接。 注:buf与buf_只是为了区别变量思考题无实验中的问题将单工通信修改为双工通信的过程中,没有注意两次变量的变化,调试的过程中花费了较多的时间。由于代码还不够完善,只能满

17、足基本的双工通信,还有很多优秀功能没有实现。由于长时间没有写代码,在看懂各个函数作用以及接口的过程中花费了较多的时间。附件1.程序源代码Client下的代码如下所示/* */ file name: socket-client.c/ file useage: demostrate the basic TCP-based blocked/ socket programming in windows system/ file origin: demo of duplex-talk/computer networks, system approach, 4th/ modified by Wei Liu

18、, 06/12/2011/* */* wliu comments: required for linux socket programming */#include /#include /#include /#include /* 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,

19、char * argv) /* wliu comments: required for windows socket programming */ WSADATA WSAData; int WSAreturn;/* wliu comments: useless pointer */ /FILE *fp; struct hostent *hp; struct sockaddr_in sin; char *host; char bufMAX_BUFSIZE; int s; int len; if (argc=2) host = argv1; else fprintf(stderr, usage:

20、duplex-talk hostn);exit(1); /* wliu comments: modified for windows socket programming */WSAreturn = WSAStartup(0 x101,&WSAData);if(WSAreturn)fprintf(stderr, duplex-talk: WSA error.n);exit(1); /* translate host name into peers IP address */ hp = gethostbyname(host); if (!hp) fprintf(stderr, duplex-ta

21、lk: 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(sin); memcpy(char *)&sin.sin_addr, hp-h_

22、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(duplex-talk: socket failed.);exit(1); if (connect(s, (struct sockaddr *)&sin, sizeof(sin) 0) perror(duplex-talk: connect failed.);/* wliu comments: mod

23、ified for windows socket programming */close(s);closesocket(s);exit(1); /* wliu comments: displaying current status */ printf(duplex-talk client connection to %s is readyn, host);/*printf(duplex-talk client please input your message (empty input to halt):n); /* wliu comments: modification to support

24、 connection termination */ /while (fgets(buf, sizeof(buf), stdin) /* main loop: get and send lines of text */ while (1) printf(duplex-talk client client is starting to talk (empty input to halt):n);fgets(buf, sizeof(buf), stdin);/* wliu comments: modified to stop sending */ if ( strlen(buf) = 1 ) /*

25、 wliu comments: user input empty message with n */buf0 = 0;send(s, buf, 1, 0);printf(duplex-talk client empty message is send to servern);break; else bufMAX_BUFSIZE-1 = 0;len = strlen(buf) + 1;send(s, buf, len, 0);printf(duplex-talk client send %d chars to servern, strlen(buf);printf(duplex-listen c

26、lient client is starting to listen. n); len = recv(s, buf, sizeof(buf), 0);if ( strlen(buf) = 0 ) printf(duplex-listen client empty message is receivedn);break; else printf(duplex-listen client received %d chars n, strlen(buf);fputs(buf, stdout); printf(duplex-talk client connection is terminatedn);

27、 /* wliu comments: modified for windows socket programming */ WSACleanup(); return 1;Socket代码如下/* */ file name: socket-server.c/ file useage: demostrate the basic TCP-based blocked/ socket programming in windows system/ file origin: demo of duplex-talk/computer networks, system approach, 4th/ modifi

28、ed by Wei Liu, 06/12/2011/* */* wliu comments: required for linux socket programming */#include /#include /#include /#include /* wliu comments: required for windows socket programming */#include #pragma comment(lib, wsock32.lib) #include #include #define SERVER_PORT 5432#define MAX_PENDING 5#define

29、MAX_BUFSIZE 256#define MAX_CONNECTION 3int main() /* wliu comments: required for windows socket programming */ WSADATA WSAData; int WSAreturn; struct sockaddr_in sin; char bufMAX_BUFSIZE; int len; int s,new_s;/* wliu comments: connections */int conn; /* wliu comments: required for windows socket pro

30、gramming */WSAreturn = WSAStartup(0 x101,&WSAData);if(WSAreturn)fprintf(stderr, duplex-talk: WSA error.n);exit(1); /* wliu comments: modified for string memory operation in windows */ /bzero(char *)&sin, sizeof(sin); /* build address data structure */ memset(char *)&sin, 0, sizeof(sin); sin.sin_fami

31、ly = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(SERVER_PORT); /* setup passive open */ if (s = socket(PF_INET, SOCK_STREAM, 0) 0) perror(duplex-talk: socket failed.);exit(1); if (bind(s, (struct sockaddr *)&sin, sizeof(sin) 0) perror(duplex-talk: bind failed.);exit(1); listen(s,

32、 MAX_PENDING); /* wliu comments: displaying current status */ printf(duplex-talk server server is ready .n); /* wait for connection, then receive and print text */conn = 0; while( conn MAX_CONNECTION ) /* wliu comments: correction for variable len */ len = sizeof(struct sockaddr_in); if (new_s = acc

33、ept(s, (struct sockaddr *)&sin, &len) 0) perror(duplex-talk: accept failed.); exit(1); /* wliu comments: displaying current status */ printf(duplex-talk server received a connection from %s : n, inet_ntoa(sin.sin_addr);/* wliu comments: modification to support connection termination */ /while (len = recv(new_s, buf,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论