【正文】
} }//if (FD_ISSET(sockfd, amp。readfds)。 rc = recv(sockfd, buffer_r, BUFF_SIZE, 0)。readfds)) /**************Client read****************/ if (FD_ISSET(sockfd, amp。 fgets(buffer_w, BUFF_SIZE, stdin)。 default: /**************Client write****************/ if (FD_ISSET(STDIN_FILENO, amp。 close(sockfd)。readfds, NULL, NULL, amp。readfds)。 = 0。 int maxfd = sockfd。 } else { fprintf(stderr, Client is trying to connect...\n)。 while(connect(sockfd, (struct sockaddr *)amp。 = AF_INET。 } sockfd = socket(AF_INET, SOCK_STREAM, 0)。i39。p39。 if(argc != 5) { fprintf(stderr, Usage:%s p server_port i server_ip\n, argv[0])。 int server_port。 struct sockaddr_in server_addr。 pthread_attr_destroy(amp。 fprintf(stderr, Master thread[%u] created a new thread failurefully\n, 23 pthread_self())。 if (ret != 0) { USER_LIST delet=delete_pointer(find_user(head,newfd), head)。 /***************Create a new slave thread**********************/ ret = pthread_create(amp。 next_pnext=creat_pointer()。 } if(head==NULL) { next_p=creat_pointer()。addrlen)。 22 bzero(amp。 int num=1。 pthread_attr_setdetachstate(amp。 pthread_attr_t attr。 err_sys(server bind) } if (listen(sockfd, BACK_LOG) == 1)//closedlisten { shutdown(sockfd, SHUT_RD | SHUT_WR)。//INADDR_ANY = htons(atoi(argv[2]))。 bzero(amp。//closed 21 if (sockfd == 1) { err_sys(server socket) } int i = 1。 socklen_t addrlen = addr_size。 } }//if (FD_ISSET(newfd, amp。readfds)。 if(delet!=NULL) head=delet。, BUFF_SIZE)。 continue。 if(delet!=NULL) head=delet。readfds, NULL, NULL, amp。readfds)。 = 0。 char buffer_r[BUFF_SIZE]。 struct timeval ts。 (3)在一臺(tái)計(jì)算機(jī)的終端先運(yùn)行服務(wù)器程序 (./multithread_tcp_server p 8080),再在客戶端的計(jì)算機(jī)終端上運(yùn)行客戶端程序 ()就會(huì)看到結(jié)果 ;運(yùn)行結(jié)果如 下所示。運(yùn)用交叉編譯工具 armlinuxgcc,執(zhí)行編譯指令生成可執(zhí)行文件。 2 發(fā)送請(qǐng)求 (1) 獲取主機(jī)信息 (2) 初始化 socket 端口 (3)利用 connect 函數(shù)講自己的 IP 地址等信息發(fā)送到主機(jī),等待主機(jī)接受信息。 Sock_fd = socket( AF_INET, SOCK_STREAM, 0) , AF_INET 為 IPV4協(xié)議, SOCK_STREAM 為使用 socket 的 流 模式 。現(xiàn)在我們介紹 socket 中的四次握手釋放連接的過程,請(qǐng)看下圖: 圖示過程為:某個(gè)應(yīng)用進(jìn)程首先調(diào)用 close 主動(dòng)關(guān)閉連接,這時(shí) TCP 發(fā)送一個(gè)FIN M;另一端接收到 FIN M 之后,執(zhí)行被動(dòng)關(guān)閉,對(duì)這個(gè) FIN 進(jìn)行確認(rèn)。 在這里須注意,服務(wù)器 并不是通過監(jiān)聽端口來與客戶連接并通信,而是產(chǎn)生一個(gè)新的套接口與客戶通信。 原型: int accept(int sockfd, void *addr,int *addrlen )。 參數(shù)說明: sockfd 是本地的 sockfd 描述符; serv_addr 是包含目的主機(jī)IP地址和端口號(hào)的指針。 功能:關(guān)閉套接字,執(zhí)行 close()之后,套接字將不再允許進(jìn)行讀操作和寫操作。protocol 通常賦值 “0” 。 (注: i_addr()返回的地方已經(jīng)是網(wǎng)絡(luò)字節(jié)順序了 ) 也可以把一個(gè) struct in_addr 代表的 IP地址打印出來(按照 數(shù)字 .數(shù)字 .數(shù)字 .數(shù)字的格式)如 printf(“%s”,i_ntoa() )。 ntohl() —— “Network to Host Long” 網(wǎng)絡(luò)字節(jié)順序轉(zhuǎn)換為主機(jī)字節(jié)順序(對(duì)無符號(hào)長型進(jìn)行操作 8 bytes)。所以對(duì)與內(nèi)部字節(jié)表示順序和網(wǎng)絡(luò)字節(jié)順序不同的機(jī)器,一定要對(duì)數(shù)據(jù)進(jìn)行轉(zhuǎn)換(比如 IP 地址的表示,端口號(hào)的表示)。 sin_family通常被賦值 AF_INET。 /* 端口號(hào) */ struct in_addr sin_addr。 sa_family 一般為 AF_INET。 Linux 支持多種套接 口地址結(jié)構(gòu),在這兒只介紹一下 IPV4 套接口地址結(jié)構(gòu)和通用套接口地址結(jié)構(gòu)。 10 TCP/IP 協(xié)議模型及各層所用協(xié)議 套接字和套接口地址結(jié)構(gòu) 套接字是套接口描述字的簡稱,是整型數(shù)字,它于文件描述符共用一段數(shù)值空間 0~ 65535。網(wǎng)絡(luò)應(yīng)用程序之間通過套接口來實(shí)現(xiàn)通信。為了保護(hù)變量,我們必須使用信號(hào)量、互斥等方法來保證我們對(duì)變量的正確使用。許多函數(shù)是不可重入的,即同時(shí)不能運(yùn)行一個(gè)函數(shù)的多個(gè)拷貝(除非使用不同的數(shù)據(jù)段)。 實(shí)現(xiàn) 線程創(chuàng)建 pthread_create() 所需頭文件 include 函數(shù)原型 int pthread_create ((pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)) thread:線程標(biāo)識(shí)符 attr:線程屬性設(shè)置 start_routine:線程函數(shù)的起始地址 arg:傳遞給 start_routine 的參數(shù) 函數(shù)返回值 成功: 0 出錯(cuò): 1 線程退出 pthread_exit()。 8 第 四 章 關(guān)鍵技術(shù)分析 linux 線程 間通信 線程是一個(gè)進(jìn)程內(nèi)的基本調(diào)度單位,也可以稱為輕量級(jí)進(jìn)程。 make 使你能不退出 gdb 就可以重新產(chǎn)生可執(zhí)行文件。 run 執(zhí)行當(dāng)前被調(diào)試的程序。 kill 終止正在調(diào)試的程序。在編譯時(shí)用 g 選項(xiàng)打開調(diào)試選項(xiàng)。也就是 Makefile 中最核心的內(nèi)容。 mand 也就是 make 需要執(zhí)行的命令。 make是一個(gè)命令工具,是一個(gè)解釋 makefile中指令的命令工 具,一般來說,大多數(shù)的 IDE 都有這個(gè)命令,比如: Delphi 的 make, Visual C++的 nmake, Linux 下 GNU 的 make。對(duì)于 A類,預(yù)處理程序 cpp 在系統(tǒng)預(yù)設(shè)包含文件目錄 (如 /usr/include)中搜尋相應(yīng)的文件,而B類,預(yù)處理程序在目標(biāo)文件的文件夾內(nèi)搜索相應(yīng)文件。 O,對(duì)程序進(jìn)行優(yōu)化編譯、連接,采用這 個(gè)選項(xiàng),整個(gè)源代碼會(huì)在編譯、連接過程中進(jìn)行優(yōu)化處理,這樣產(chǎn)生的可執(zhí)行文件的執(zhí)行效率可以提高,但是,編譯、連接的速度就相應(yīng)地要慢一些。 c,只編譯,不連接成為可執(zhí)行文件,編譯器只是由輸入的 .c 等源代碼文件生成 .o 為后綴的目標(biāo)文件,通常用于編譯不包含主程序的子程序文件。在連接階段,所有的目標(biāo)文件被安排在可執(zhí)行程序中的恰當(dāng)?shù)奈恢茫瑫r(shí),該程序所調(diào)用到的庫函數(shù)也從各自所在的檔案庫中連到合適的地方。 命令 gcc 首先調(diào)用 cpp 進(jìn)行預(yù)處理,在預(yù)處理過程中, 對(duì)源代碼文件中的文件包含 (include)、預(yù)編譯語句 (如 宏 定義 define 等 )進(jìn)行分析。 GCC 原名為 GNU C 語言編譯器,因?yàn)樗局荒芴幚? C語言??蛻魻顟B(tài)分為鏈接客戶和非連接客戶,我采用結(jié)構(gòu)體存儲(chǔ)鏈接客戶信息,以鏈表來記錄鏈接客戶。 服務(wù)器主要是處理客戶輸入信息。 第二章 linux 網(wǎng)絡(luò)聊天室 設(shè)計(jì)思想 功能分析 本系統(tǒng)是想在 linux 系統(tǒng)下實(shí)現(xiàn)一個(gè)聊天室系統(tǒng),只要有服務(wù)器和客戶端。 目前計(jì)算機(jī)網(wǎng)絡(luò)持續(xù)而高速地發(fā)展 ,其中基于 TCP/ IP 協(xié)議網(wǎng)絡(luò)已經(jīng)成為計(jì)算機(jī)之間組網(wǎng)的常見形式 . 基于 TCP/ IP 的網(wǎng)絡(luò)編程 ,也得到了廣泛的應(yīng) 4 用。再有,且其內(nèi)核小、效率高、兼容性好和穩(wěn)定性強(qiáng)等優(yōu)點(diǎn)。 單就網(wǎng)絡(luò)編程來說, linux 和 windows 其實(shí)都差不多,只是基于不同的開發(fā)平臺(tái),并且 linux的 API基本上是自己用 C寫的,而 windows底層有封裝, 只要熟悉 linux網(wǎng)絡(luò)編程,如果以后轉(zhuǎn) windows 相對(duì)容易。 關(guān)鍵字: TCP/IP; linux; socket Design and Implementation of Inter chat rooms under Linux Linren Ming Of Computer Science, Communication Engineering 2021 Instructor: Yue Miao Abstract: This system uses a bination of C / S structure, between the client and the client and customer client and serverside through the TCP / IP protocolbased Socket sets of interfaces to deliver messages. Used in server design and implementation of multithreading technology, in a single program which is running a number of different threads to perform different tasks. Greatly enhanced the program on the use of server resources. To write and debug serverside program and client program under Linux, the connection between the client, server and munications. Two windows can be opened up on a standalone client and server, or the two hosts links, respectively, as the client and server. Overall, this design embedded C language for the development of language, the use of work sockets 2 socket connection is established and run multithreaded data exchange program GCC debugging can be used in standalone work. Keywords: TCP / IP。大大增強(qiáng)了程序?qū)Ψ?wù)器資源的利用。服務(wù)器設(shè)計(jì)與實(shí)現(xiàn)過程中 采用了多線程技術(shù),可以在單個(gè)程序當(dāng)中同時(shí)運(yùn)行多個(gè)不 同的線程 執(zhí)行不同的任務(wù)??傮w來