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

时间:2021-04-19 18:39:17

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

服务器端:

 // File Name: process_server.c
// Author: jiujue
// Created Time: 2019年03月10日 星期日 20时29分18秒 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include <unistd.h>
#include <sys/socket.h>
#include <ctype.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
#include <pthread.h> typedef struct sock_info
{
int m_fd;
pthread_t m_pthid;
struct sockaddr_in m_addr; }Sock_info; void* works(void* arg)
{
Sock_info *c_info = (Sock_info*)arg; //obtain client info
int client_port = ntohs(c_info->m_addr.sin_port);
char client_Ip[]; socklen_t buf_len = sizeof(client_Ip); inet_ntop(AF_INET, (void*)&c_info->m_addr.sin_addr.s_addr,client_Ip,buf_len); printf("\t\t\t\t Ip %s, port %d,connect successful\n",client_Ip,client_port); while()
{
char buf[] = {};
int read_len = read(c_info->m_fd, buf, sizeof(buf));
if(read_len > )
{
buf[read_len+]='\n';
printf("->-> Obtain if of ,Ip %s, port %d, send info: %s\n",client_Ip,client_port,buf);
write(c_info->m_fd,buf,strlen(buf));
}
else if( == read_len)
{
printf("\t\t\t\t Ip %s, port %d, disconnect\n",client_Ip,client_port);
break;
}
else if(- == read_len)
{
perror("read error");
exit();
}
} return NULL;
} int main(int argc, const char* argv[])
{
if(argc < )
{
printf("eg: ./app IP Port\n");
exit();
}
short port = atoi(argv[]); int lfd = socket(AF_INET, SOCK_STREAM, );
if(- == lfd)
{
perror("socket error");
exit();
} struct sockaddr_in serv;
serv.sin_port = htons(port);
serv.sin_family = AF_INET;
inet_pton(AF_INET, argv[], &serv.sin_addr.s_addr); int res = bind(lfd, (struct sockaddr *)&serv, sizeof(serv));
if(- == res)
{
perror("bind error");
exit();
} res = listen(lfd, );
if(- == res)
{
perror("listen error");
exit();
} while()
{
printf("\a Wait accepting...\n");
struct sockaddr_in client_add; socklen_t len = sizeof(client_add); int cfd = accept(lfd,(struct sockaddr*)&client_add, &len); while(- == cfd && cfd == EINTR)
{
cfd = accept(lfd,(struct sockaddr*)&client_add, &len);
} // supply son pthread info
Sock_info* s_info =(Sock_info*)malloc(sizeof(Sock_info)); s_info->m_fd = cfd;
s_info->m_addr = client_add; int res = pthread_create(&s_info->m_pthid, NULL, works, (void*)s_info);
if(res == -)
{
perror("pthread_creat error");
exit();
}
pthread_detach(s_info->m_pthid); } close(lfd);
return ;
}

客户端:

 // File Name: socket_client.c
// Author: jiujue
// Created Time: 2019年03月09日 星期六 13时00分05秒 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h> int main(int argc, const char* argv[])
{ if(argc < )
{
printf("eg: ./app Ip Port\n");
exit();
}
int port = atoi(argv[]); int fd = socket(AF_INET, SOCK_STREAM, );
if(- == fd)
{
perror("socket error");
exit();
} struct sockaddr_in serv; serv.sin_port = htons(port);
serv.sin_family = AF_INET;
inet_pton(AF_INET, argv[], &serv.sin_addr.s_addr); socklen_t len = sizeof(serv);
int res = connect(fd,(struct sockaddr *) &serv, len);
if(- == res)
{
perror("connect error");
exit();
}
printf("Connectt server successful!!\n"); while()
{
printf("Please input string\n>>");
char buf[];
fgets(buf,sizeof(buf),stdin);
write(fd, buf, strlen(buf));
printf("send buf: %s\n",buf);
len = read(fd,buf,(buf));
if(len > )
{
printf("Recv buf: %s\n",buf);
}else if(len == )
{
printf("Serer disconnect \n");
break;
}
else if(- == len)
{
perror("Read errror");
exit();
}else
{
printf("I no now\n");
}
} close(fd);
return ;
}

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

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