嵌入式系統(tǒng)及應用qt綜合實驗報告_第1頁
嵌入式系統(tǒng)及應用qt綜合實驗報告_第2頁
嵌入式系統(tǒng)及應用qt綜合實驗報告_第3頁
嵌入式系統(tǒng)及應用qt綜合實驗報告_第4頁
嵌入式系統(tǒng)及應用qt綜合實驗報告_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

安徽工業(yè)大學嵌入式系統(tǒng)及應用綜合實驗報告Linux系統(tǒng)下TFTP和QT下聊天程序設計姓名:學號:專業(yè):年級:指導教師:2011年課前資料:參考資料查閱工具:man,如manls,manmake;C編寫手冊,機器上“l(fā)inuxc參考〞;Shell編寫手冊,機器上“l(fā)inuxshell參考“;Makefile編寫手冊,機器上“makefile參考“;Awk編寫手冊,機器上“awk參考“;經(jīng)典書籍,學習linux,unix的最正確書籍“Linux,unixshell編程指南“實驗題目:熟悉linux操作系統(tǒng)下最簡單實用的通信程序socket.最好能全部完成,否那么按照完成情況打分。實驗目的:通過對socket的編寫,可以了解linux下最簡單實用的進程通信方法,為后續(xù)信號燈、消息隊列等學習奠定根底。實驗設備及環(huán)境:1.硬件設備:PC機一臺2.軟件環(huán)境:安裝Linux操作系統(tǒng),并安裝相關的程序開發(fā)環(huán)境,如C\C++\tsh\bsh等編程語言環(huán)境。實驗內容及要求:〔1〕用C語言編程實現(xiàn)linux簡單的聊天室功能。用戶程序命名為client.c;效勞器程序命名為server.c綁定端口等信息見實驗方法內容;要求client可以通過socket連接server在client,提示輸入效勞器ip假設連接server的socket建立成功,返回提示信息Client輸入的聊天內容在client端〔多個client端〕和server端同時顯示;多個client可同時接入server,進入聊天室,最多支持20個client;Client端輸入quit退出連接,server端提示client退出。可選擇使用多線程實現(xiàn)多客戶端;其他細節(jié)見輸出結果;五、實驗方法內容需要的頭文件#include<stdlib.h>#include<stdio.h>#include<string.h>#include<errno.h>#include<netdb.h>#include<sys/types.h>#include<netinet/in.h>#include<sys/socket.h>#include<sys/wait.h>#include<unistd.h>#include<pthread.h>主要的常量變量客戶端:#defineTRUE1#definePORT5000intquit=0;//quit表示是否用戶確定退出效勞器端:#defineMAXLINE1000//在一條消息中最大的輸出字符數(shù)#defineLISTENQ20//最大監(jiān)聽隊列#definePORT5000//監(jiān)聽端口#defineMAXFD20//最大的在線用戶數(shù)量void*get_client(void*);inti,maxi=-1;//maxi表示當前client數(shù)組中最大的用戶的i值intclient[MAXFD];主要模塊客戶端:intmain(void)void*get_server(void*sockfd)//get_server函數(shù),用于接受效勞器轉發(fā)的消息效勞器端:intmain()void*get_client(void*sockfd)//運行get_client函數(shù),處理用戶請求六.代碼考慮大家沒做過,給幾個例子:參考socket編程.pdf/*******客戶端程序client.c************/#include<stdlib.h>#include<stdio.h>#include<errno.h>#include<string.h>#include<netdb.h>#include<sys/types.h>#include<netinet/in.h>#include<sys/socket.h>#defineTRUE1#definePORT5000staticintsockfd;voidrecvfromserver()//接受效勞器消息線程入口函數(shù){ charmes[1024]; intnbytes=0; while(1) { memset(mes,0,sizeof(mes)); nbytes=read(sockfd,mes,sizeof(mes)); if(nbytes>0) { mes[nbytes]='\0'; printf("%s\n",mes); } } pthread_exit(NULL);}intmain(intargc,char*argv[]){// intsockfd; charbuffer[1024]; structsockaddr_inserver_addr; structhostent*host; intportnumber,nbytes; charstrhost[16]; charclientname[20]; charmes[1024]; intthr_id;/*threadIDforthenewlycreatedthread*/ pthread_tp_thread;/*thread'sstructure*/ if(argc!=1) { fprintf(stderr,"Usage:%s\a\n",argv[0]); exit(1); } printf("請輸入效勞器ip地址\n"); scanf("%s",strhost); if((host=gethostbyname(strhost))==NULL) { fprintf(stderr,"Gethostnameerror\n"); exit(1); } /*客戶程序開始建立sockfd描述符*/ printf("正在建立套接口...\n"); if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"SocketError:%s\a\n",strerror(errno)); exit(1); } /*客戶程序填充效勞端的資料*/ bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_port=htons(PORT); server_addr.sin_addr=*((structin_addr*)host->h_addr); printf("套接口創(chuàng)立成功,正在鏈接效勞器...\n"); /*客戶程序發(fā)起連接請求*/ if(connect(sockfd,(structsockaddr*)(&server_addr),sizeof(structsockaddr))==-1) { fprintf(stderr,"ConnectError:%s\a\n",strerror(errno)); exit(1); } /*連接成功了*/ printf("鏈接效勞器成功\n歡送來到聊天室\n"); printf("請輸入你的用戶昵稱\n"); scanf("%s",clientname);// write(sockfd,clientname,sizeof(clientname)); printf("\n\n開始聊天吧〔\"Quit\"斷開連接〕\n\n"); thr_id=pthread_create(&p_thread,NULL,recvfromserver,NULL); while(1) { memset(buffer,0,sizeof(buffer)); memset(mes,0,sizeof(mes)); scanf("%s",buffer); strcat(mes,clientname); strcat(mes,":"); strcat(mes,buffer); // printf("mainthread%s\n",mes); if((write(sockfd,mes,sizeof(mes)))==-1) { fprintf(stderr,"WriteError:%s\n",strerror(errno)); exit(1); } if(strcmp(buffer,"Quit")==0) { break; } } /*結束通訊*/ close(sockfd); exit(0);}/*******效勞器程序(server.c)************/#include<stdlib.h>#include<stdio.h>#include<errno.h>#include<string.h>#include<netdb.h>#include<sys/types.h>#include<netinet/in.h>#include<sys/socket.h>#defineMAXLINE1000//在一條消息中最大的輸出字符數(shù)#defineLISTENQ20//最大監(jiān)聽隊列#definePORT5000//監(jiān)聽端口#defineMAXFD20//最大的在線用戶數(shù)量void*get_client(void*);intsockfd,i;staticintmaxi=0;//maxi表示當前client數(shù)組中最大的用戶的i值staticintclient[MAXFD];voidrecvandsend(void)//監(jiān)聽轉發(fā)線程入口函數(shù){ intindex=0; intnbytes=0; charbuffer[1024]; intlen; intoutindex=0; while(1) { if(maxi>0) { memset(buffer,0,sizeof(buffer)); nbytes=0; //index++; nbytes=read(client[index++],buffer,sizeof(buffer));// printf("%d,%d\n",index,client[index]); if(nbytes>0) { buffer[nbytes]='\0'; printf("%s\n",buffer); outindex=0; while(outindex<maxi) if(write(client[outindex++],buffer,sizeof(buffer))==-1) { fprintf(stderr,"WriteError:%s\n",strerror(errno)); exit(1); } } } if(index>=maxi) index=0; } pthread_exit(NULL);}intmain(intargc,char*argv[]){// intclient_fd[LISTENQ],clientnum=0;; structsockaddr_inserver_addr; structsockaddr_inclient_addr; intsin_size,portnumber; charhello[]="Hello!AreYouFine?\n"; intthr_id;/*threadIDforthenewlycreatedthread*/ pthread_tp_thread;/*thread'sstructure*/ intnew_fd=0; memset(client,0,sizeof(client)); if(argc!=1) { fprintf(stderr,"Usage:%sportnumber\a\n",argv[0]); exit(1); } /*效勞器端開始建立socket描述符*/ if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socketerror:%s\n\a",strerror(errno)); exit(1); } /*效勞器端填充sockaddr結構*/ bzero(&server_addr,sizeof(structsockaddr_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,(structsockaddr*)(&server_addr),sizeof(structsockaddr))==-1) { fprintf(stderr,"Binderror:%s\n\a",strerror(errno)); exit(1); } printf("效勞器監(jiān)聽端口%d...\n",PORT); /*監(jiān)聽sockfd描述符*/ if(listen(sockfd,LISTENQ)==-1) { fprintf(stderr,"Listenerror:%s\n\a",strerror(errno)); exit(1); } thr_id=pthread_create(&p_thread,NULL,recvandsend,NULL); printf("歡送來到本聊天室\n"); while(1) { /*效勞器阻塞,直到客戶程序建立連接*/ if(maxi>=20) { printf("以到達人數(shù)上線\n"); continue; } sin_size=sizeof(structsockaddr_in); if((new_fd=accept(sockfd,(structsockaddr*)(&client_addr),&sin_size))==-1) { fprintf(stderr,"Accepterror:%s\n\a",strerror(errno)); exit(1); } /*fprintf(stderr,"Servergetconnectionfrom%s\n",inet_ntoa(client_addr.sin_addr));*/ client[maxi++]=new_fd; printf("\n新用戶進入聊天室%d\n",new_fd); } close(sockfd); exit(0);}七、實驗結果執(zhí)行結果效勞器翻開客戶

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論