C语言 linux环境基于socket的简易即时通信程序
生活随笔
收集整理的這篇文章主要介紹了
C语言 linux环境基于socket的简易即时通信程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載請注明出處:http://www.cnblogs.com/kevince/p/3891033.html? ? ? ——By Kevince
最近在看linux網絡編程相關,現學現賣,就寫了一個簡易的C/S即時通信程序,代碼如下:
head.h
1 /*頭文件,client和server編譯時都需要使用*/ 2 #include <unistd.h> 3 #include <stdio.h> 4 #include <sys/types.h> 5 #include <sys/socket.h> 6 #include <string.h> 7 #include <pthread.h> 8 #include <stdlib.h> 9 #include <netinet/in.h> 10 #include <arpa/inet.h> 11 12 #define MSGLEN 1000 13 #define IPLEN 15 14 15 typedef int SOCKET;server.c:
1 /*server*/ 2 3 #include "head.h" 4 5 char msg_recv[MSGLEN], msg_send[MSGLEN]; 6 SOCKET server_sockfd, client_sockfd; 7 8 void *thread_function(void *argv) /*線程函數*/ 9 { 10 while(1){ 11 gets(msg_send); 12 write(client_sockfd, msg_send, MSGLEN); 13 } 14 pthread_exit(NULL); 15 } 16 17 int main(int arg, char *argv[]) 18 { 19 int server_len, client_len; 20 struct sockaddr_in server_address; 21 struct sockaddr_in client_address; 22 int port; 23 int res; 24 25 pthread_t a_thread; 26 void *thread_result; 27 28 if (arg != 2){ 29 printf("server --portnum\n"); 30 exit(EXIT_FAILURE); 31 } 32 33 sscanf(argv[1], "%d", &port); /*讀入端口*/ 34 35 server_sockfd = socket(AF_INET, SOCK_STREAM, 0); 36 server_address.sin_family = AF_INET; 37 server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); 38 server_address.sin_port = htons(port); 39 40 server_len = sizeof(server_address); 41 bind(server_sockfd, (struct sockaddr *)&server_address, server_len); /*綁定端口并監聽*/ 42 listen(server_sockfd, 10); 43 printf("listen...\n"); 44 45 client_len = sizeof(client_address); 46 client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len); 47 printf("connection success!\n"); 48 49 res = pthread_create(&a_thread, NULL, thread_function, NULL); /*啟動線程函數*/ 50 if (res != 0){ 51 perror("Thread creation failed"); 52 exit(EXIT_FAILURE); 53 } 54 55 while(read(client_sockfd, msg_recv, MSGLEN)){ 56 printf("msg from client: %s\n", msg_recv); 57 } 58 close(client_sockfd); 59 exit(EXIT_SUCCESS); 60 }?
client.c:
1 /*client*/ 2 3 #include "head.h" 4 5 char msg_recv[MSGLEN],msg_send[MSGLEN]; 6 SOCKET sockfd; 7 8 void *thread_function(void *argv) /*線程函數*/ 9 { 10 while(1){ 11 gets(msg_send); 12 write(sockfd, msg_send, MSGLEN); 13 } 14 pthread_exit(NULL); 15 } 16 17 int main(int arg, char *argv[]) 18 { 19 struct sockaddr_in address; 20 int len; 21 int res; 22 int port; 23 char ip[IPLEN]; 24 25 void *thread_result; 26 pthread_t a_thread; 27 28 sockfd = socket(AF_INET, SOCK_STREAM, 0); 29 30 if (arg != 3){ 31 printf("client --ipaddress --portnum\n"); 32 exit(EXIT_FAILURE); 33 } 34 35 sscanf(argv[1], "%s", ip); 36 sscanf(argv[2], "%d", &port); /*讀取ip與端口*/ 37 38 address.sin_family = AF_INET; 39 address.sin_addr.s_addr = inet_addr(ip); 40 address.sin_port = htons(port); 41 42 len = sizeof(address); 43 res = connect(sockfd, (struct sockaddr *)&address, len); 44 if (res == -1){ 45 perror("connect failed! "); 46 exit(EXIT_FAILURE); 47 } 48 printf("connection success!\n"); 49 50 res = pthread_create(&a_thread, NULL, thread_function, NULL); /*啟動線程函數*/ 51 if (res != 0){ 52 perror("Thread creation failed"); 53 exit(EXIT_FAILURE); 54 } 55 56 while(read(sockfd, msg_recv, MSGLEN)){ 57 printf("msg from server: %s\n", msg_recv); 58 } 59 res = pthread_join(a_thread, &thread_result); 60 if (res != 0){ 61 perror("joined failed"); 62 exit(EXIT_FAILURE); 63 } 64 exit(EXIT_SUCCESS); 65 }?
?
由于使用了線程,所以要鏈接正確的線程庫,所以編譯命令如下:
gcc -D_REENTRANT -I/usr/include/nptl server.c -o server -L/usr/lib/nptl -lpthreadgcc -D_REENTRANT -I/usr/include/nptl client.c -o client -L/usr/lib/nptl -lpthread?
如果你的系統默認使用的就是NPTL線程庫,那么編譯時就無需加上-I和-L選項
?
運行時輸入的命令規則是:
./server --portnum #即server后面要加上需要綁定的端口號。./client --ip --portnum #即client后面要加上服務器的IP地址以及端口號。?
?
不積跬步無以至千里,雖然這兩個程序很簡單,但作為我的第一個linux環境下基于socket的通信程序,也很有紀念意義。
轉載于:https://www.cnblogs.com/kevince/p/3891033.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的C语言 linux环境基于socket的简易即时通信程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu 1800 (map)
- 下一篇: Linux操作系统备份之二:通过tar拷