TCP协议多进程多线程并发服务器
TCP多进程多线程并发服务器
1.多进程并发服务器
#include <myhead.h>
#define SERPORT 6666
#define SERIP "192.168.0.136"
#define BLACKLOG 10
void hande(int a)
{
if(a==SIGCHLD)
{
while(waitpid(-1,NULL,WNOHANG)!=-1);//回收僵尸进程
}
}
int main(int argc, const char *argv[])
{
if(signal(SIGCHLD,hande)==SIG_ERR)//捕获子进程退出的信号
{
perror("signal");
return -1;
}
//1.创建原始套接字
int oldfd = socket(AF_INET,SOCK_STREAM,0);//IPV4,TCP通信方式
if(oldfd ==-1)
{
perror("socket");
return -1;
}
//设置套接字属性,端口号快速复用
int k=666;
if(setsockopt(oldfd,SOL_SOCKET,SO_REUSEADDR,&k,sizeof(k))==-1)
{
perror("setsockopt");
return -1;
}
//2.绑定IP和端口
struct sockaddr_in sin={
.sin_family=AF_INET,
.sin_port=htons(SERPORT),
.sin_addr.s_addr=inet_addr(SERIP)
};
if(bind(oldfd,(struct sockaddr *)&sin,sizeof(sin))==-1)
{
perror("bind");
return -1;
}
//3.监听
if(listen(oldfd,BLACKLOG)==-1)
{
perror("listen");
return -1;
}
//4.循环接受客户端请求,创建进程
struct sockaddr_in cin;
int cinlen=sizeof(cin);
while(1)
{
int newfd=accept(oldfd,(struct sockaddr *)&cin,&cinlen);//接受客户端信息(IP和端口号)
if(newfd==-1)
{
perror("accept");
return -1;
}
printf("%s:%d连接成功\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
pid_t pid=fork();//创建进程
if(pid>0)//
{
close(newfd);//关闭上一次退出的文件描述符
}
else if(pid==0)
{
char buff[1024];
while(1)
{
int len=recv(newfd,buff,sizeof(buff),0);//接受客户端发送来的信息
if(len==0)
{
printf("客户端退出\n");
break;
}
printf("服务器收到的消息:%s\n",buff);
strcat(buff,"/succ");
send(newfd,buff,sizeof(buff),0);
printf("发送成功\n");
close(newfd);//关闭新的文件描述符
exit(0);//退出子进程
}
}
else
{
perror("fork");
return -1;
}
}
close(oldfd);
return 0;
}
2.多线程并发服务器
#include <myhead.h>
#define SERPORT 6666
#define SERIP "192.168.0.136"
#define BLACKLOG 10
typedef struct
{
int newfd;//存储新文件描述符
struct sockaddr_in cin;//存储客户端信息
}New;
void *fun1(void *a)
{
int newfd = ((New *)a)->newfd;
struct sockaddr_in cin = ((New *)a)->cin;//解析客户端信息
char buff[1024];
while(1)
{
memset(buff,0,sizeof(buff));
int len = recv(newfd,buff,sizeof(buff),0);
printf("收到%s:%d的信息:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buff);//输出客户端信息
if(len==0)
{
printf("客户端退出\n");
break;
}
strcat(buff,"/succ");//回个信息
send(newfd,buff,sizeof(buff),0);
printf("发送成功\n");
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//1.创建原始套接字
int oldfd = socket(AF_INET,SOCK_STREAM,0);//IPV4,TCP通信方式
if(oldfd ==-1)
{
perror("socket");
return -1;
}
//设置套接字属性,端口号快速复用
int k=666;
if(setsockopt(oldfd,SOL_SOCKET,SO_REUSEADDR,&k,sizeof(k))==-1)
{
perror("setsockopt");
return -1;
}
//2.绑定IP和端口
struct sockaddr_in sin={
.sin_family=AF_INET,
.sin_port=htons(SERPORT),
.sin_addr.s_addr=inet_addr(SERIP)
};
if(bind(oldfd,(struct sockaddr *)&sin,sizeof(sin))==-1)
{
perror("bind");
return -1;
}
//3.监听
if(listen(oldfd,BLACKLOG)==-1)
{
perror("listen");
return -1;
}
struct sockaddr_in cin;
int cinlen=sizeof(cin);
New msg;
while(1)
{
int newfd=accept(oldfd,(struct sockaddr *)&cin,&cinlen);//接受客户端信息(IP和端口号)
if(newfd==-1)
{
perror("accept");
return -1;
}
printf("%s:%d连接成功\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
msg.newfd=newfd;
msg.cin=cin;
pthread_t tid;
if(pthread_create(&tid,NULL,fun1,&msg)!=0)//创建线程,传入参数结构体
{
perror("pthread_create");
return -1;
}
pthread_detach(tid);//挂起由系统回收线程
}
close(oldfd);
return 0;
}
客户端:
#include <myhead.h>
#define CLIPORT 6666
#define CLIIP "192.168.0.136"
int main(int argc, const char *argv[])
{
int oldfd = socket(AF_INET,SOCK_STREAM,0);//1、创建原始套节字
if(oldfd==-1)
{
perror("socket");
return -1;
}
//2、bind可选
//3、连接服务器
struct sockaddr_in cin = {
.sin_family = AF_INET,//IPV4通信
.sin_port = htons(CLIPORT),//端口号转为网络字节序
.sin_addr.s_addr = inet_addr(CLIIP)//IP地址转为网络字节序
};
if(connect(oldfd,(struct sockaddr *)&cin,sizeof(cin))==-1)
{
perror("connect");
return -1;
}
printf("连接服务器成功\n");
//发送消息
char buff[1024];
while(1)
{
memset(buff,0,sizeof(buff));
printf("客户端发信息:");
fgets(buff,sizeof(buff),stdin);
buff[strlen(buff)-1] = '\0';
send(oldfd,buff,sizeof(buff),0);//阻塞发送
recv(oldfd,buff,sizeof(buff),0);//阻塞接收
printf("收到服务器的消息:%s\n",buff);
}
close(oldfd);
return 0;
}
广播发送接收端
广播发送端:采用setsockopt 设置网络属性,为允许广播
#include <myhead.h>
#define SENDPORT 6666
#define SENDIP "192.168.0.255"//广播IP地址
int main(int argc, const char *argv[])
{
//1、创建套接字
int oldfd = socket(AF_INET,SOCK_DGRAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//2、设置套接字允许广播
int k=2;
if(setsockopt(oldfd,SOL_SOCKET,SO_BROADCAST,&k,sizeof(k))==-1)
{
perror("setsockopt");
return -1;
}
printf("设置允许广播成功\n");
struct sockaddr_in send = {
.sin_family = AF_INET,
.sin_port = htons(SENDPORT),
.sin_addr.s_addr = inet_addr(SENDIP)
};
struct sockaddr_in rec;
int reclen = sizeof(rec);
char buff[1024];
//3、发送信息
while(1)
{
memset(buff,0,sizeof(buff));
sleep(5);
strcat(buff,"/successful");
sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&send,sizeof(send));
printf("发送成功\n");
}
close(oldfd);//关闭套接字
return 0;
}
广播接收端:
#include <myhead.h>
#define SENDPORT 6666
#define SENDIP "192.168.0.255"//广播IP地址
int main(int argc, const char *argv[])
{
//1、创建套接字
int oldfd = socket(AF_INET,SOCK_DGRAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//绑定
struct sockaddr_in rec = {
.sin_family = AF_INET,
.sin_port = htons(SENDPORT),
.sin_addr.s_addr = inet_addr(SENDIP)
};
if(bind(oldfd,(struct sockaddr *)&ree,sizeof(ree))==-1)
{
perror("bind");
return -1;
}
char buff[1024];
while(1)
{
bzero(buff,sizeof(buff));
recvfrom(oldfd,buff,sizeof(buff),0,NULL,NULL);//接收消息
printf("%s\n",buff);
strcat(buff,"/suc");
sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&rec,sizeof(rec));
printf("发送成功\n");
}
return 0;