欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ubuntu环境下实现 多线程的socket(tcp) 通信

程序员文章站 2022-03-28 19:44:44
改改就是个小型局域网聊天 服务器端: 1 // File Name: process_server.c 2 // Author: jiujue 3 // Created Time: 2019年03月10日 星期日 20时29分18秒 4 5 #include 6 #include< ......

改改就是个小型局域网聊天

服务器端:

ubuntu环境下实现 多线程的socket(tcp) 通信
  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 }
view code

客户端:

ubuntu环境下实现 多线程的socket(tcp) 通信
 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 }
view code

 

 结语:有问题欢迎提在下方 ,本人在校学生,时间较为充裕, 有时间会回复的。

/* 原创文章 转载请附上原链接: https://www.cnblogs.com/jiujue/p/10513859.html   */