改改就是个小型局域网聊天
服务器端:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 // File Name: process_server.c 2 // Author: jiujue 3 // Created Time: 2019年03月10日 星期日 20时29分18秒 4 5 #include<stdio.h> 6 #include<string.h> 7 #include<stdlib.h> 8 #include<time.h> 9 #include<math.h> 10 #include <unistd.h> 11 #include <sys/socket.h> 12 #include <ctype.h> 13 #include <sys/types.h> 14 #include <arpa/inet.h> 15 #include <signal.h> 16 #include <sys/wait.h> 17 #include <errno.h> 18 #include <pthread.h> 19 20 typedef struct sock_info 21 { 22 int m_fd; 23 pthread_t m_pthid; 24 struct sockaddr_in m_addr; 25 26 }Sock_info; 27 28 void* works(void* arg) 29 { 30 Sock_info *c_info = (Sock_info*)arg; 31 32 //obtain client info 33 int client_port = ntohs(c_info->m_addr.sin_port); 34 char client_Ip[64]; 35 36 socklen_t buf_len = sizeof(client_Ip); 37 38 inet_ntop(AF_INET, (void*)&c_info->m_addr.sin_addr.s_addr,client_Ip,buf_len); 39 40 printf("\t\t\t\t Ip %s, port %d,connect successful\n",client_Ip,client_port); 41 42 while(1) 43 { 44 char buf[1024] = {0}; 45 int read_len = read(c_info->m_fd, buf, sizeof(buf)); 46 if(read_len > 0) 47 { 48 buf[read_len+1]='\n'; 49 printf("->-> Obtain if of ,Ip %s, port %d, send info: %s\n",client_Ip,client_port,buf); 50 write(c_info->m_fd,buf,strlen(buf)); 51 } 52 else if(0 == read_len) 53 { 54 printf("\t\t\t\t Ip %s, port %d, disconnect\n",client_Ip,client_port); 55 break; 56 } 57 else if(-1 == read_len) 58 { 59 perror("read error"); 60 exit(1); 61 } 62 } 63 64 return NULL; 65 } 66 67 int main(int argc, const char* argv[]) 68 { 69 if(argc < 3) 70 { 71 printf("eg: ./app IP Port\n"); 72 exit(1); 73 } 74 short port = atoi(argv[2]); 75 76 int lfd = socket(AF_INET, SOCK_STREAM, 0); 77 if(-1 == lfd) 78 { 79 perror("socket error"); 80 exit(1); 81 } 82 83 struct sockaddr_in serv; 84 serv.sin_port = htons(port); 85 serv.sin_family = AF_INET; 86 inet_pton(AF_INET, argv[1], &serv.sin_addr.s_addr); 87 88 int res = bind(lfd, (struct sockaddr *)&serv, sizeof(serv)); 89 if(-1 == res) 90 { 91 perror("bind error"); 92 exit(1); 93 } 94 95 res = listen(lfd, 128); 96 if(-1 == res) 97 { 98 perror("listen error"); 99 exit(1); 100 } 101 102 103 while(1) 104 { 105 printf("\a Wait accepting...\n"); 106 struct sockaddr_in client_add; 107 108 socklen_t len = sizeof(client_add); 109 110 int cfd = accept(lfd,(struct sockaddr*)&client_add, &len); 111 112 while(-1 == cfd && cfd == EINTR) 113 { 114 cfd = accept(lfd,(struct sockaddr*)&client_add, &len); 115 } 116 117 // supply son pthread info 118 Sock_info* s_info =(Sock_info*)malloc(sizeof(Sock_info)); 119 120 s_info->m_fd = cfd; 121 s_info->m_addr = client_add; 122 123 int res = pthread_create(&s_info->m_pthid, NULL, works, (void*)s_info); 124 if(res == -1) 125 { 126 perror("pthread_creat error"); 127 exit(1); 128 } 129 pthread_detach(s_info->m_pthid); 130 131 } 132 133 close(lfd); 134 return 0 ; 135 }
客户端:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 // File Name: socket_client.c 2 // Author: jiujue 3 // Created Time: 2019年03月09日 星期六 13时00分05秒 4 5 #include<stdio.h> 6 #include<string.h> 7 #include<stdlib.h> 8 #include<time.h> 9 #include<math.h> 10 #include <unistd.h> 11 #include <sys/socket.h> 12 #include <arpa/inet.h> 13 14 15 int main(int argc, const char* argv[]) 16 { 17 18 if(argc < 3) 19 { 20 printf("eg: ./app Ip Port\n"); 21 exit(1); 22 } 23 int port = atoi(argv[2]); 24 25 int fd = socket(AF_INET, SOCK_STREAM, 0); 26 if(-1 == fd) 27 { 28 perror("socket error"); 29 exit(1); 30 } 31 32 struct sockaddr_in serv; 33 34 serv.sin_port = htons(port); 35 serv.sin_family = AF_INET; 36 inet_pton(AF_INET, argv[1], &serv.sin_addr.s_addr); 37 38 socklen_t len = sizeof(serv); 39 int res = connect(fd,(struct sockaddr *) &serv, len); 40 if(-1 == res) 41 { 42 perror("connect error"); 43 exit(1); 44 } 45 printf("Connectt server successful!!\n"); 46 47 while(1) 48 { 49 printf("Please input string\n>>"); 50 char buf[1024]; 51 fgets(buf,sizeof(buf),stdin); 52 write(fd, buf, strlen(buf)); 53 printf("send buf: %s\n",buf); 54 len = read(fd,buf,(buf)); 55 if(len > 0) 56 { 57 printf("Recv buf: %s\n",buf); 58 }else if(len == 0) 59 { 60 printf("Serer disconnect \n"); 61 break; 62 } 63 else if(-1 == len) 64 { 65 perror("Read errror"); 66 exit(1); 67 }else 68 { 69 printf("I no now\n"); 70 } 71 } 72 73 close(fd); 74 return 0 ; 75 }
结语:有问题欢迎提在下方 ,本人在校学生,时间较为充裕, 有时间会回复的。
/* 原创文章 转载请附上原链接: https://www.cnblogs.com/jiujue/p/10513859.html */