




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、操作系統(tǒng)實 驗 報 告課程名稱操作系統(tǒng)實驗學(xué)期 2011 至 2012 學(xué)年 第一 學(xué)期學(xué)生所在系部通信工程系年級 2008 專業(yè)班級 010812班學(xué)生姓名路遙學(xué)號 01081191 任課教師陳晨實驗成績通信工程系制課前資料:參考資料查閱工具:man,如 man ls,man make; c 編寫手冊,機(jī)器上“l(fā)inux c 參考 ” ; shell 編寫手冊,機(jī)器上“l(fā)inux shell 參考“;makefile 編寫手冊,機(jī)器上“makefile 參考“;awk 編寫手冊,機(jī)器上“awk 參考“;經(jīng)典書籍,學(xué)習(xí)linux,unix 的最佳書籍“l(fā)inux ,unix shell 編程指
2、南“一、實驗題目:熟悉 linux 操作系統(tǒng)下最簡單實用的通信程序socket.最好能全部完成,否則按照完成情況打分。二、實驗?zāi)康模和ㄟ^對 socket 的編寫,可以了解linux 下最簡單實用的進(jìn)程通信方法,為后續(xù)信號燈、消息隊列等學(xué)習(xí)奠定基礎(chǔ)。三、實驗設(shè)備及環(huán)境:1. 硬件設(shè)備: pc 機(jī)一臺2. 軟件環(huán)境:安裝linux 操作系統(tǒng),并安裝相關(guān)的程序開發(fā)環(huán)境,如c c+tshbsh 等編程語言環(huán)境。四、實驗內(nèi)容及要求:(1)用 c 語言編程實現(xiàn)linux 簡單的聊天室功能。用戶程序命名為client.c; 服務(wù)器程序命名為server.c 綁定端口等信息見實驗方法內(nèi)容;要求 client
3、可以通過socket 連接 server 在 client,提示輸入服務(wù)器ip 若連接 server 的 socket 建立成功,返回提示信息client 輸入的聊天內(nèi)容在client 端(多個client 端)和 server 端同時顯示;多個 client 可同時接入server,進(jìn)入聊天室,最多支持20 個 client;client 端輸入 quit 退出連接, server 端提示 client 退出。可選擇使用多線程實現(xiàn)多客戶端;其他細(xì)節(jié)見輸出結(jié)果;五、實驗方法內(nèi)容1. 需要的頭文件#include #include #include #include #include #incl
4、ude #include #include #include #include #include 2. 主要的常量變量客戶端:#define true 1 #define port 5000 int quit=0; /quit表示是否用戶確定退出服務(wù)器端:#define maxline 1000 /在一條消息中最大的輸出字符數(shù)#define listenq 20 /最大監(jiān)聽隊列#define port 5000 /監(jiān)聽端口#define maxfd 20 /最大的在線用戶數(shù)量void *get_client(void *); int i,maxi=-1;/maxi表示當(dāng)前 client數(shù)組中最
5、大的用戶的i 值int clientmaxfd; 3. 主要模塊客戶端:int main(void) void *get_server(void* sockfd) /get_server函數(shù),用于接受服務(wù)器轉(zhuǎn)發(fā)的消息服務(wù)器端:int main() void *get_client(void *sockfd) /運行 get_client函數(shù),處理用戶請求六代碼考慮大家沒做過,給幾個例子:參考 socket 編程 .pdf /* 客戶端程序client.c */ #include #include #include #include #include #include #include #inc
6、lude #define true 1 #define port 5000 static int sockfd; void recvfromserver() / 接受服務(wù)器消息線程入口函數(shù) char mes1024; int nbytes=0; while(1) memset(mes,0,sizeof(mes); nbytes=read(sockfd,mes,sizeof(mes); if(nbytes0) mesnbytes=0; printf(%sn,mes); pthread_exit(null); int main(int argc, char *argv) / int sockfd;
7、 char buffer1024; struct sockaddr_in server_addr; struct hostent *host; int portnumber,nbytes; char strhost16; char clientname20; char mes1024; int thr_id; /* thread id for the newly created thread */ pthread_t p_thread; /* threads structure */ if(argc!=1) fprintf(stderr,usage:%s an,argv0); exit(1);
8、 printf( 請輸入服務(wù)器ip 地址 n); scanf(%s,strhost); if(host=gethostbyname(strhost)=null) fprintf(stderr,gethostname errorn); exit(1); /* 客戶程序開始建立sockfd 描述符*/ printf( 正在建立套接口.n); if(sockfd=socket(af_inet,sock_stream,0)=-1) fprintf(stderr,socket error:%san,strerror(errno); exit(1); /* 客戶程序填充服務(wù)端的資料*/ bzero(&am
9、p;server_addr,sizeof(server_addr); server_addr.sin_family=af_inet; server_addr.sin_port=htons(port); server_addr.sin_addr=*(struct in_addr *)host-h_addr); printf( 套接口創(chuàng)建成功,正在鏈接服務(wù)器.n); /* 客戶程序發(fā)起連接請求*/ if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)=-1) fprintf(stderr,co
10、nnect error:%san,strerror(errno); exit(1); /* 連接成功了*/ printf( 鏈接服務(wù)器成功n歡迎來到聊天室n); printf( 請輸入你的用戶昵稱n); scanf(%s,clientname); / write(sockfd,clientname,sizeof(clientname); printf(nn開始聊天吧(quit斷開連接) nn); thr_id = pthread_create(&p_thread, null, recvfromserver, null); while(1) memset(buffer,0,sizeof(
11、buffer); memset(mes,0,sizeof(mes); scanf(%s,buffer); strcat(mes,clientname); strcat(mes,:); strcat(mes,buffer); / printf(main thread %sn,mes); if(write(sockfd,mes,sizeof(mes)=-1) fprintf(stderr,write error:%sn,strerror(errno); exit(1); if(strcmp(buffer,quit)=0) break; /* 結(jié)束通訊*/ close(sockfd); exit(0
12、); /* 服務(wù)器程序(server.c) */ #include #include #include #include #include #include #include #include #define maxline 1000 / 在一條消息中最大的輸出字符數(shù)#define listenq 20 / 最大監(jiān)聽隊列#define port 5000 /監(jiān)聽端口#define maxfd 20 /最大的在線用戶數(shù)量void *get_client(void *); int sockfd,i; static int maxi=0;/maxi表示當(dāng)前client 數(shù)組中最大的用戶的i 值sta
13、tic int clientmaxfd; void recvandsend(void) / 監(jiān)聽轉(zhuǎn)發(fā)線程入口函數(shù) int index=0; int nbytes=0; char buffer1024; int len; int outindex=0; while(1) if(maxi0) memset(buffer,0,sizeof(buffer); nbytes=0; /index+; nbytes=read(clientindex+,buffer,sizeof(buffer); / printf(%d,%dn,index,clientindex); if(nbytes0) buffernb
14、ytes=0; printf( %sn,buffer); outindex=0; while(outindex=maxi) index=0; pthread_exit(null); int main(int argc, char *argv) / int client_fdlistenq,clientnum=0; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size,portnumber; char hello=hello! are you fine?n; int thr_id; /* thre
15、ad id for the newly created thread */ pthread_t p_thread; /* threads structure */ int new_fd=0; memset(client,0,sizeof(client); if(argc!=1) fprintf(stderr,usage:%s portnumberan,argv0); exit(1); /* 服務(wù)器端開始建立socket 描述符*/ if(sockfd=socket(af_inet,sock_stream,0)=-1) fprintf(stderr,socket error:%sna,strer
16、ror(errno); exit(1); /* 服務(wù)器端填充sockaddr 結(jié)構(gòu)*/ bzero(&server_addr,sizeof(struct sockaddr_in); server_addr.sin_family=af_inet; server_addr.sin_addr.s_addr=htonl(inaddr_any); server_addr.sin_port=htons(port); /* 捆綁sockfd 描述符*/ if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockadd
17、r)=-1) fprintf(stderr,bind error:%sna,strerror(errno); exit(1); printf( 服務(wù)器監(jiān)聽端口%d.n,port); /* 監(jiān)聽sockfd 描述符*/ if(listen(sockfd,listenq)=-1) fprintf(stderr,listen error:%sna,strerror(errno); exit(1); thr_id = pthread_create(&p_thread, null, recvandsend, null); printf( 歡迎來到本聊天室n); while(1) /* 服務(wù)器阻塞
18、 ,直到客戶程序建立連接*/ if(maxi=20) printf( 以達(dá)到人數(shù)上線n); continue; sin_size=sizeof(struct sockaddr_in); if(new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size)=-1) fprintf(stderr,accept error:%sna,strerror(errno); exit(1); /*fprintf(stderr,server get connection from %sn,inet_ntoa(client_addr.sin_addr);*/ clientmaxi+=new_fd; printf(n新用戶進(jìn)入聊天室%dn,new_fd); close(sockfd); exit(0); 七、實驗結(jié)果1. 執(zhí)行結(jié)果服務(wù)器打開客戶端打開,并輸入了地址,昵稱服務(wù)器端顯示客戶端 2 進(jìn)入服務(wù)器顯示張三輸入李四端顯示服務(wù)器顯示李四輸入張三顯示服務(wù)器顯示2. 結(jié)果分析這是一個聊天室程序, 可以實現(xiàn)群聊的功能, 即當(dāng)某個客戶發(fā)出消息后, 服務(wù)器和其他個客
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 醫(yī)院科研過程管理制度
- 公司門禁密碼管理制度
- 大漢集團(tuán)薪酬管理制度
- 單位涉案財物管理制度
- 小區(qū)綠化水泵管理制度
- 員工設(shè)備工具管理制度
- 壓鑄行業(yè)安全管理制度
- 計算機(jī)三級考試新思潮試題及答案
- 嵌入式軟件測試方法試題及答案
- 普通高等學(xué)校本科專業(yè)設(shè)置申請表
- 多發(fā)性硬化康復(fù)
- 醫(yī)用高數(shù)課后習(xí)題答案
- 開封中學(xué)教師招聘2022年考試真題及答案解析二2
- GB/T 41837-2022溫泉服務(wù)溫泉水質(zhì)要求
- RB/T 017-2019低碳產(chǎn)品評價方法與要求三相配電變壓器
- JJG 30-2012通用卡尺
- GB/T 26785-2011細(xì)水霧滅火系統(tǒng)及部件通用技術(shù)條件
- 中藥藥理學(xué)題庫(附上答案)
- GB/T 21695-2008飼料級沸石粉
- 客戶溝通與交流課件
評論
0/150
提交評論