多进程
客户端
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main() {
//创建套接字
int lfd = socket(AF_INET, SOCK_STREAM, 0);
if(lfd == -1) {
perror("socket");
exit(-1);
}
//连接服务器
struct sockaddr_in serveraddr;
inet_pton(AF_INET, "192.168.245.129", &serveraddr.sin_addr.s_addr);
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(9999);
int ret = connect(lfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr));
if(ret == -1) {
perror("connect");
_exit(-1);
}
//通信
int i = 0;
char receiveBuf[1024] = {0};
while(1) {
sprintf(receiveBuf, "data: %d\n", i++);
write(lfd, receiveBuf, strlen(receiveBuf));
sleep(1);
int len = read(lfd, receiveBuf, sizeof(receiveBuf));
if(len == -1) {
perror("read");
exit(0);
} else if(len > 0) {
printf("receive server data: %s\n", receiveBuf);
} else if (len == 0) {
printf("server closed...");
}
}
close(lfd);
}
服务器端
#define _XOPEN_SOURCE
#include<stdio.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdlib.h>
#include<signal.h>
#include<wait.h>
#include<errno.h>
void recyleChild(int arg) {
while(1) {
int ret = waitpid(-1, NULL, WNOHANG);
if(ret == -1) {
break;
} else if (ret == 0) {
break;
} else if (ret > 0) {
printf("pid %d is resaved\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(AF_INET, SOCK_STREAM, 0);
if(lfd == -1) {
perror("socket");
exit(0);
}
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(0);
}
//监听
ret = listen(lfd, 128);
if(ret == -1) {
perror("listen");
exit(0);
}
//接受链接
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 clientIP[16];
inet_ntop(AF_INET, &cliaddr.sin_addr.s_addr, clientIP, 16);
unsigned short cliPort = ntohs(cliaddr.sin_port);
printf("Client Port is %d, client addr is %s\n", cliPort, clientIP);
char recvBuf[1024] = {0};
while(1) {
int len = read(cfd, &recvBuf, sizeof(recvBuf));
if(len == -1) {
perror("read");
return 0;
} else if(len > 0) {
printf("recv client data : %s \n", recvBuf);
} else {
printf("closed.....");
}
write(cfd, &recvBuf, sizeof(recvBuf));
}
close(cfd);
exit(0);
}
}
close(lfd);
return 0;
}
多线程
服务器文章来源:https://www.toymoban.com/news/detail-728168.html
#define _XOPEN_SOURCE
#include<stdio.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<pthread.h>
struct sockinfo {
int fd;
struct sockaddr_in saddr;
pthread_t tid;
};
struct sockinfo sockinfos[128];
void* working(void* arg) {
struct sockinfo* pinfo = (struct sockinfo*) arg;
char clientIP[16];
inet_ntop(AF_INET, &pinfo->saddr.sin_addr.s_addr, clientIP, 16);
unsigned short cliPort = ntohs(pinfo->saddr.sin_port);
printf("Client Port is %d, client addr is %s\n", cliPort, clientIP);
char recvBuf[1024] = {0};
while(1) {
int len = read(pinfo->fd, &recvBuf, sizeof(recvBuf));
if(len == -1) {
perror("read");
return 0;
} else if(len > 0) {
printf("recv client data : %s \n", recvBuf);
} else {
printf("closed.....");
}
write(pinfo->fd, &recvBuf, sizeof(recvBuf));
}
close(pinfo->fd);
exit(0);
return NULL;
}
int main() {
//创建socket
int lfd = socket(AF_INET, SOCK_STREAM, 0);
if(lfd == -1) {
perror("socket");
exit(0);
}
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(0);
}
//监听
ret = listen(lfd, 128);
if(ret == -1) {
perror("listen");
exit(0);
}
//初始化数据
int max = sizeof(sockinfos) / sizeof(sockinfos[0]);
for(int i = 0; i < max; i++) {
memset(&sockinfos[i], 0, 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);
if(cfd == -1) {
perror("accept");
exit(-1);
}
struct sockinfo * pinfo;
for(int i = 0; i < max; i++) {
if(sockinfos[i].fd == -1) {
pinfo = &sockinfos[i];
break;
}
if(i == max - 1) {
sleep(1);
i--;
}
}
pinfo->fd = cfd;
memcpy(&pinfo->saddr, &cliaddr, len);
//每连接一个,创建一个子进程
pthread_create(&pinfo->tid, NULL, working, pinfo);
pthread_detach(pinfo->tid);
}
close(lfd);
return 0;
}
客户端文章来源地址https://www.toymoban.com/news/detail-728168.html
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main() {
//创建套接字
int lfd = socket(AF_INET, SOCK_STREAM, 0);
if(lfd == -1) {
perror("socket");
exit(-1);
}
//连接服务器
struct sockaddr_in serveraddr;
inet_pton(AF_INET, "192.168.245.129", &serveraddr.sin_addr.s_addr);
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(9999);
int ret = connect(lfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr));
if(ret == -1) {
perror("connect");
_exit(-1);
}
//通信
int i = 0;
char receiveBuf[1024] = {0};
while(1) {
sprintf(receiveBuf, "data: %d\n", i++);
write(lfd, receiveBuf, strlen(receiveBuf));
sleep(1);
int len = read(lfd, receiveBuf, sizeof(receiveBuf));
if(len == -1) {
perror("read");
exit(0);
} else if(len > 0) {
printf("receive server data: %s\n", receiveBuf);
} else if (len == 0) {
printf("server closed...");
}
}
close(lfd);
}
到了这里,关于Linux TCP 通信并发的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!