设为首页 加入收藏

TOP

ubuntu环境下实现 多线程的socket(tcp) 通信(一)
2019-03-12 00:09:12 】 浏览:450
Tags: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

客户端:

 1 // File Name: socket_client.c
 2
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇#leetcode刷题之路18-四数之和 下一篇内核中对文件的读写操作

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目