一、客户端
代码如下:
// 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 saddr;
inet_pton(AF_INET, "182.61.11.197", &saddr.sin_addr.s_addr);
saddr.sin_family = AF_INET;
saddr.sin_port = htons(9999);
int ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
if(ret==-1)
{
perror("connet");
exit(-1);
}
//3.通信
char recvbuf[1024] = {
0};
int i = 0;
while (1)
{
sprintf(recvbuf, "%d\n", i++);
write(fd, recvbuf, strlen(recvbuf)+1);
int lens = read(fd, recvbuf, sizeof(recvbuf));
if(lens==-1)
{
perror("read");
exit(-1);
}else if(lens>0)
{
printf("recv server data: %s\n", recvbuf);
}else if(lens==0)
{
//表示客户端断开连接
printf("server closed...");
}
sleep(1);
}
//关闭连接
close(fd);
return 0;
}
二、服务器
1.多进程
代码如下:
#include<stdio.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.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("子进程 %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 ret = bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr));
if(ret==-1)
{
perror("bind");
exit(-1);
}
//监听
ret = listen(lfd, 5);
if(ret==-1)
{
perror("listen");
exit(-1);
}
//不断循环等待客户端连接
while(1)
{
struct sockaddr_in caddr;
int len = sizeof(caddr);
//接受连接
int cfd = accept(lfd, (struct sockaddr *)&caddr, &len);
if(cfd==-1)
{
if(errno == EINTR)
{
continue;
}
perror("accept");
exit(-1);
}
//每一个连接进来,创建一个子进程跟客户端通信
pid_t pid = fork();
if(pid==0)
{
//子进程
//获取客户端的信息
char cip[16];
inet_ntop(AF_INET, &caddr.sin_addr.s_addr, cip, sizeof(cip));
unsigned short cport = ntohs(caddr.sin_port);
printf("client ip is :%s, port is %d\n", cip, cport);
//接受客户端发来的数据
char recvbuf[1024];
while(1)
{
int len = read(cfd, &recvbuf, sizeof(recvbuf));
if(len == -1)
{
perror("read");
exit(-1);
}else if(len > 0)
{
printf("recv client data: %s\n", recvbuf);
}else
{
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<stdlib.h>
#include<string.h>
#include<sys/types.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 cip[16];
inet_ntop(AF_INET, &pinfo->addr.sin_addr.s_addr, cip, sizeof(cip));
unsigned short cport = ntohs(pinfo->addr.sin_port);
printf("client ip is :%s, port is %d\n", cip, cport);
//接受客户端发来的数据
char recvbuf[1024];
while(1)
{
int len = read(pinfo->fd, &recvbuf, sizeof(recvbuf));
if(len == -1)
{
perror("read");
exit(-1);
}else if(len > 0)
{
printf("recv client data: %s\n", recvbuf);
}else
{
printf("client closed...\n");
break;
}
write(pinfo->fd, recvbuf, strlen(recvbuf)+1);
}
close(pinfo->fd);
}
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, 5);
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 caddr;
int len = sizeof(caddr);
//接受连接
int cfd = accept(lfd, (struct sockaddr *)&caddr, &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=-1;
}
}
pinfo->fd=cfd;
memcpy(&pinfo->addr, &caddr, len);
//创建子线程
pthread_create(&pinfo->tid, NULL, working, pinfo);
pthread_detach(pinfo->tid);
}
close(lfd);
return 0;
}
总结
本节主要介绍了TCP实现并发服务器。主要运用了多线程和多进程的知识点。
文章评论