第一次實(shí)驗(yàn)試驗(yàn)報(bào)告_第1頁
第一次實(shí)驗(yàn)試驗(yàn)報(bào)告_第2頁
第一次實(shí)驗(yàn)試驗(yàn)報(bào)告_第3頁
第一次實(shí)驗(yàn)試驗(yàn)報(bào)告_第4頁
第一次實(shí)驗(yàn)試驗(yàn)報(bào)告_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、華中科技大學(xué) 電子與信息工程系實(shí)驗(yàn)報(bào)告- PAGE 19 - PAGE 1 -電子與信息工程系實(shí) 驗(yàn) 報(bào) 告實(shí)驗(yàn)名稱Windows Socket 編程課程名稱計(jì)算機(jī)網(wǎng)絡(luò)姓名張煌澤學(xué)號U201013030日期2012-3-27地點(diǎn)南一樓東204成績教師徐晶老師實(shí)驗(yàn)?zāi)康?掌握網(wǎng)絡(luò)應(yīng)用程序的開發(fā)方法;2掌握Client/ Server 結(jié)構(gòu)軟件的設(shè)計(jì)與開發(fā)方法3掌握Socket 機(jī)制的工作原理實(shí)驗(yàn)環(huán)境運(yùn)行的操作系統(tǒng):windows XP,VC 6.0實(shí)驗(yàn)內(nèi)容與結(jié)果軟件編程類實(shí)驗(yàn)報(bào)告內(nèi)容:1演示單工過程(省略截圖,重點(diǎn)討論雙工)2修改完成通信雙工過程程序整體功能;實(shí)驗(yàn)整體實(shí)現(xiàn)將原來只能由client接

2、受到socket傳來的數(shù)據(jù),改成可以將socket和client兩端可以互相發(fā)送和接受數(shù)據(jù)。程序組成及各模塊/函數(shù)功能;函數(shù)功能主體是實(shí)現(xiàn)發(fā)送和接受雙方的相互傳送數(shù)據(jù)的代碼主體功能的源代碼分為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);/添加部分結(jié)束 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編程實(shí)現(xiàn)雙工通信過程,即client給server發(fā)送消息,同時server可以給client回復(fù)消息,并能在雙方窗口有消息顯示。程序組成及各模塊/函數(shù)功能;程序分為兩邊1.client:socket:創(chuàng)建“插座”(client名為new_s)使兩邊可以連接Gethostbyname:根據(jù)主機(jī)名獲取ip地址,便于連接。Connect:向server發(fā)送連接socket_s及socket_new_s的請求。Send:發(fā)送報(bào)

16、文(名為buf)Recv:接收開來自server的報(bào)文(名為buf_)Closesocket:關(guān)閉連接。2.server:socket:創(chuàng)建“插座”(server名為s)使兩邊可以連接Bind:把socket綁定到具體地址Listen:監(jiān)聽等待client的接入。Accept:接受connect的連接請求。Send:發(fā)送報(bào)文(名為buf_)Recv:接收開來自server的報(bào)文(名為buf)Closesocket:關(guān)閉連接。 注:buf與buf_只是為了區(qū)別變量思考題無實(shí)驗(yàn)中的問題將單工通信修改為雙工通信的過程中,沒有注意兩次變量的變化,調(diào)試的過程中花費(fèi)了較多的時間。由于代碼還不夠完善,只能滿

17、足基本的雙工通信,還有很多優(yōu)秀功能沒有實(shí)現(xiàn)。由于長時間沒有寫代碼,在看懂各個函數(shù)作用以及接口的過程中花費(fèi)了較多的時間。附件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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論