原文:Linux下C語言多線程,網絡通訊簡單聊天程序html
功能描述:程序應用多線程技術,但是實現1對N進行網絡通訊聊天。但至今沒想出合適的退出機制,除了用Ctr+C。出於演示目的,這裏採用UNIX域協議(文件系統套接字),程序分爲客戶端和服務端。應用select函數來實現異步的讀寫操做。服務器
先說一下服務端:首先先建立套接字,而後綁定,接下進入一個無限循環,用accept函數,接受「鏈接」請求,而後調用建立線程函數,創造新的線程,進入下一個循環。這樣每當有一個新的「鏈接」被接受都會建立一個新的線程,實現1對N的網絡通訊。在服務端程序中線程中用一個buffer讀寫,爲了不錯誤,這時就要給關鍵代碼加上互斥鎖work_mutex,具體見代碼。網絡
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<pthread.h> #include<sys/socket.h> #include<sys/un.h> #include<unistd.h> #include<semaphore.h> //這裏沒有用二進制信號量能夠刪掉 char buffer[1024]; //讀寫用的區域 sem_t bin_sem; //沒用到的二進制信號量,能夠刪掉 void *pthread_function(void *arg); //線程入口函數聲明 pthread_mutex_t work_mutex; //聲明互斥鎖 int main(){ int result; //整數變量用來儲存調用函數的返回值 struct sockaddr_un server_address, client_address; //UNIX域的套接字,server_address用於服務端的監聽,client_address用於客戶端鏈接後的套接字 int client_len; //鏈接後,accept函數會把客戶端的地址的長度儲存在這 int server_socketfd, client_socketfd;//服務端和客戶端的套接字文件描述符 pthread_t a_thread; //線程ID標誌 pthread_attr_t thread_attr; //線程的屬性,後面能夠看的,被我註釋掉了,沒用到,能夠刪掉。 result = sem_init(&bin_sem, 0, 1); //初始化二進制信號量,由於用了互斥鎖,因此沒用到,能夠刪掉 if(result != 0){ perror("sem_init"); exit(EXIT_FAILURE); } result = pthread_mutex_init(&work_mutex, NULL);//初始化互斥鎖 if(result != 0){ perror("pthread_mutex_init"); exit(EXIT_FAILURE); } server_socketfd = socket(AF_UNIX, SOCK_STREAM, 0);//建立套接字,用TCP鏈接方式,出於演示目的只用UNIX域套接字。 server_address.sun_family = AF_UNIX; strcpy(server_address.sun_path, "server_socket"); unlink("server_socket"); //在綁定以前,把之前存在當前目錄下的套接字刪除 result = bind(server_socketfd, (struct sockaddr*)&server_address, sizeof(server_address)); //綁定 if(result != 0){ perror("bind"); exit(EXIT_FAILURE); } result = listen(server_socketfd, 5);//監聽,最多容許5個鏈接請求 if(result != 0){ perror("listen"); exit(EXIT_FAILURE); } client_len = sizeof(client_address); while(1){ //開始進入無限循環 /* printf("If you want to quit, please enter 'quit'\n"); printf("Do you want to accept a connectiong\n"); memset(buffer, '\0', sizeof(buffer)); fgets(buffer, sizeof(buffer), stdin); if((strncmp("quit", buffer, 4))==0) break; */ client_socketfd = accept(server_socketfd, (struct sockaddr*)&client_address, &client_len); //接受一個鏈接請求 /* result = pthread_attr_init(&thread_attr); if(result != 0){ perror("pthread_attr_init"); exit(EXIT_FAILURE); } result = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); if(result != 0){ perror("pthread_attr_setdetachstate"); exit(EXIT_FAILURE); } */ result = pthread_create(&a_thread, NULL, pthread_function, (void *)client_socketfd); //成功接受一個請求後,就會建立一個線程,而後主線程又進入accept函數,若是此時沒有鏈接請求,那麼主線程會阻塞 if(result != 0){ perror("pthread_create"); exit(EXIT_FAILURE); } } } void *pthread_function(void *arg){ //線程入口函數,每調用一次pthread_create,都會建立一個新的線程 int fd = (int) arg; //把函數參數,即鏈接成功後的套接字,賦給fd. int result; fd_set read_fds; //文件描述符集合,用於select函數 int max_fds; //文件描述符集合的最大數 printf("%d id has connected!!\n", fd); while (1){ FD_ZERO(&read_fds);//清空集合 FD_SET(0, &read_fds);//將標準輸入放入監聽的文件描述符集合, 這個用於讀取標準輸入,即鍵盤的輸入 FD_SET(fd, &read_fds);//將鏈接後的客戶文件描述符放入監聽的文件描述符集合, 這個用於向客戶端讀取數據 max_fds = fd + 1; // sem_wait(&bin_sem); pthread_mutex_lock(&work_mutex); //對關鍵區域上鎖 printf("%d has get the lock\n", fd); result = select(max_fds, &read_fds, (fd_set *)NULL, (fd_set *)NULL, (struct timeval*)NULL); //開始監聽那些文件描述符出於可讀狀態 if(result < 1){ printf("select"); } if(FD_ISSET(0, &read_fds)){ //若是標準輸入處於可讀狀態,說明鍵盤有所輸入,將輸入的數據存放在buffer中,而後向客戶端寫回,若是輸入「quit」將會退出一個聊天線程 memset(buffer, '\0', sizeof(buffer)); //保險起見,清零 fgets(buffer, sizeof(buffer), stdin); if((strncmp("quit", buffer, 4))==0){ printf("You have terminaled the chat\n"); // sem_post(&bin_sem); pthread_mutex_unlock(&work_mutex); break; } else{ result=write(fd, buffer, sizeof(buffer)); if(result==-1){ perror("write"); exit(EXIT_FAILURE); } } } if(FD_ISSET(fd, &read_fds)){ //若是客戶套接字符可讀,那麼讀取存放在buffer中,而後顯示出來,若是對方中斷聊天,那麼result==0 memset(buffer, '\0', sizeof(buffer)); result = read(fd, buffer, sizeof(buffer)); if(result == -1){ perror("read"); exit(EXIT_FAILURE); } else if(result == 0){ printf("The other side has terminal the chat\n"); // sem_post(&bin_sem); pthread_mutex_unlock(&work_mutex); break; } else{ printf("receive message: %s", buffer); } } pthread_mutex_unlock(&work_mutex); //解鎖 sleep (1); //若是沒有這一行,當前線程會一直佔據buffer.讓當前線程暫停一秒能夠實現1對N的功能。 // sem_post(&bin_sem); // sleep (1); } // printf("I am here\n"); close(fd); pthread_exit(NULL); }
讀者能夠對比一下http://blog.csdn.net/hwz119/archive/2007/03/19/1534233.aspx多線程
讀者能夠發現,連接網絡中的程序須要結束當前一個聊天才能進行下一個聊天,而這個服務端能夠同時對N我的進行聊天,儘管有些bug(若是客戶端對方回覆太快太頻繁,服務端的鎖就會切換來切換去,沒法回覆到正確的客戶端)。異步
客戶端跟服務端很像,但比較簡單。這裏面就不註釋了。這兩個程序我都運行過。。。沒什麼基本大的問題。。可是功能很不完善。。。還需改進。。。。。socket
#include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<sys/un.h> #include<string.h> #include<sys/types.h> #include<sys/time.h> int main() { int result; int socketfd; int len; struct sockaddr_un address; fd_set read_fds, test_fds; int fd; int max_fds; char buffer[1024]; socketfd = socket(AF_UNIX, SOCK_STREAM, 0); address.sun_family = AF_UNIX; strcpy(address.sun_path, "server_socket"); len = sizeof(address); result = connect(socketfd, (struct sockaddr*)&address, len); if(result == -1) { perror("connect"); exit(EXIT_FAILURE); } FD_ZERO(&read_fds); FD_SET(0, &read_fds); FD_SET(socketfd, &read_fds); max_fds = socketfd +1; printf("Chat now!!\n"); while(1) { test_fds = read_fds; result = select(max_fds, &test_fds, (fd_set *)NULL, (fd_set *)NULL, (struct timeval*)NULL); if(result < 1) { perror("select"); exit(EXIT_FAILURE); } if(FD_ISSET(0, &test_fds)) { memset(buffer, '\0', sizeof(buffer)); // printf("send:"); fgets(buffer, sizeof(buffer), stdin); if((strncmp("quit", buffer, 4))== 0) { printf("\nYou are going to quit\n"); break; } result = write(socketfd, buffer, sizeof(buffer)); if(result == -1) { perror("write"); exit(EXIT_FAILURE); } } if(FD_ISSET(socketfd, &test_fds)) { memset(buffer, '\0', sizeof(buffer)); result = read(socketfd, buffer, sizeof(buffer)); if(result == -1) { perror("read"); exit(EXIT_FAILURE); } else if(result == 0) { printf("The other side has termianl chat!\n"); break; } else { printf("recieve: %s", buffer); } } } close(socketfd); exit(EXIT_SUCCESS); }