1.多进程版
服务端
#include<stdio.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<signal.h>
#include<wait.h>
#include<errno.h>
void recyleChild(int ard) {
while(1){
int ret = waitpid(-1, NULL, WNOHANG);
if(ret == -1){
break;
}else if(ret == 0){
break;
}else if(ret > 0){
printf("子进程 %d 被回收\n",ret);
}
}
}
int main() {
struct sigaction act;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
act.sa_handler = recyleChild;
//注册信号捕捉
sigaction(SIGCHLD, &act, NULL);
//创建socket
int lfd = socket(PF_INET, SOCK_STREAM, 0);
if(lfd == -1){
perror("socket");
exit(-1);
}
//绑定
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(9999);
saddr.sin_addr.s_addr = INADDR_ANY;
int optval = 1;
setsockopt(lfd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));
//端口复用
int ret = bind(lfd, (struct sockaddr *)&saddr, sizeof (saddr));
if(ret ==-1){
perror("bind");
exit(-1);
}
//监听
ret = listen(lfd, 128);
if(ret == -1){
perror("listen");
exit(-1);
}
//不断循环等待客户端连接
while(1){
//接受链接
struct sockaddr_in cliaddr;
int len= sizeof(cliaddr);
int cfd = accept(lfd, (struct sockaddr *)&cliaddr, &len);
if(cfd == -1){
if(errno == EINTR){
continue;
}
perror("accept");
exit(-1);
}
//每一个连接进来,创建一个子进程跟客户端通信
pid_t pid = fork();
if(pid == 0){
//子进程
//获取客户端信息
char cliIp[16];
inet_ntop(AF_INET, &cliaddr.sin_addr.s_addr, cliIp, sizeof(cliIp));
unsigned short cliPort = ntohs(cliaddr.sin_port);
printf("client ip is: %s, port is %d\n",cliIp, cliPort);
//接受客户端发来的数据
char recvBuf[1024]={0};
while(1){
int len =recv(cfd, &recvBuf, sizeof(recvBuf), 0);
if(len==-1){
perror("recv");
exit(-1);
}else if(len>0){
printf("recv client : %s\n",recvBuf);
}else if(len == 0){
printf("client closed.....\n");
break;
}
write(cfd, recvBuf, strlen(recvBuf)+1);
}
close(cfd);
exit(0);
}
}
close(lfd);
return 0;
}
2.多线程版
服务端
#include<stdio.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>
struct sockInfo {
int fd; //通信的文件描述符
pthread_t tid; //线程号
struct sockaddr_in addr;
};
struct sockInfo sockinfos[128];
void * working(void * arg){
//子线程和客户端通信 cfd 客户端的信息
//获取客户端信息
struct sockInfo * pinfo = (struct sockInfo *)arg;
char cliIp[16];
inet_ntop(AF_INET, &pinfo->addr.sin_addr.s_addr, cliIp, sizeof(cliIp));
//将网络传输的二进制数值转化为成点分十进制的ip地址
unsigned short cliPort = ntohs(pinfo->addr.sin_port);
//将一个无符号短整形数从网络字节顺序转换为主机字节顺序。
printf("client ip is: %s, port is %d\n",cliIp, cliPort);
//接受客户端发来的数据
char recvBuf[1024]={0};
while(1){
int len =read(pinfo->fd, &recvBuf, sizeof(recvBuf));
if(len==-1){
perror("read");
exit(-1);
}else if(len>0){
printf("recv client : %s\n",recvBuf);
}else if(len == 0){
printf("client closed.....\n");
break;
}
write(pinfo->fd, recvBuf, strlen(recvBuf)+1);
}
close(pinfo->fd);
return NULL;
}
int main() {
//创建socket
int lfd = socket(PF_INET, SOCK_STREAM, 0);
if(lfd == -1){
perror("socket");
exit(-1);
}
//绑定
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(9999);//端口号
saddr.sin_addr.s_addr = INADDR_ANY;
int ret = bind(lfd, (struct sockaddr *)&saddr, sizeof (saddr));
if(ret ==-1){
perror("bind");
exit(-1);
}
//监听
ret = listen(lfd, 128);
if(ret == -1){
perror("listen");
exit(-1);
}
//初始化数据
int max =sizeof(sockinfos) / sizeof(sockinfos[0]);
for(int i=0;i<max;i++){
bzero(&sockinfos[i], sizeof(sockinfos[i]));
sockinfos[i].fd = -1;
sockinfos[i].tid = -1;
}
//循环等待客户端连接
while(1) {
//接受链接
struct sockaddr_in cliaddr;
int len= sizeof(cliaddr);
int cfd = accept(lfd, (struct sockaddr *)&cliaddr, &len);
//创建子线程
struct sockInfo * pinfo;
for(int i=0;i<max;i++){
//从这个数组中找到一个可用的sockInfo元素
if(sockinfos[i].fd == -1){
pinfo = &sockinfos[i];
break;
}
if(i == max-1){
sleep(1);
i--;
}
}
pinfo->fd=cfd;
memcpy(&pinfo->addr, &cliaddr, len);
pthread_create(&pinfo->tid, NULL, working, pinfo);
pthread_detach(pinfo->tid);//线程分离,结束自己释放资源
}
close(lfd);
return 0;
}
客户端
//TCP通信客户端
#include<stdio.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int main() {
//1.创建套接字
int fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd == -1){
perror("socket");
exit(-1);
}
//2.连接服务器
struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
inet_pton(AF_INET, "192.168.110.135", &serveraddr.sin_addr.s_addr);
serveraddr.sin_port = htons(9999);
int ret = connect(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
if(ret == -1){
perror("connect");
exit(-1);
}
char recvBuf[1024]={0};
//3.通信
int i=0;
while(1){
// char data[1024];
fgets(recvBuf, sizeof(recvBuf), stdin);
write(fd, recvBuf, strlen(recvBuf)+1);
// i++;
int len = read(fd, recvBuf, sizeof(recvBuf));
if(len == -1){
perror("read");
exit(-1);
}else if(len > 0){
printf("recv serve : %s\n",recvBuf);
}else if(len==0){
printf("serve close...\n");
break;
}
sleep(1);
}
//关闭
close(fd);
return 0;
}
标签:recvBuf,include,socket,int,ret,len,Linux,多线程,struct From: https://www.cnblogs.com/wxk1213/p/16898118.html