创建一个多进程服务器和多线程服务器
多进程服务器
#include
//服务端
#define PORT 8888
#define IP "192.168.0.105"
void handler(int signo)
{
if(signo==SIGCHLD)
{
while(waitpid(-1,NULL,WNOHANG)>0);
}
}
int deal_msg(int nfd,struct sockaddr_in cin)
{
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
int res=recv(nfd,rbuf,sizeof(rbuf),0);
if(res==0)
{
printf("客户端下线\n");
break;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);
strcat(rbuf,":)");
send(nfd,rbuf,sizeof(rbuf),0);
}
close(nfd);
}
int main(int argc, const char *argv[])
{
//1.创建套接字文件
int sfd=-1;
if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket error");
return -1;
}
//1.1端口号快速重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
}
//2.绑定ip地址,端口号
//2.1填充服务端地址信息结构体
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
//3.将套接字设置成监听状态
if(listen(sfd,128)==-1)
{
perror("listen error");
return -1;
}
//4.接受客户端连接请求
int nfd=-1;
struct sockaddr_in cin;
socklen_t socklen=sizeof(cin);
pid_t pid=-1;
if(signal(SIGCHLD,handler)==SIG_ERR)
{
perror("signal error");
return -1;
}
while(1)
{
if((nfd=accept(sfd,(struct sockaddr *)&cin,&socklen))==-1)
{
perror("accept error");
return -1;
}
printf("[%s:%d]accept success,nfd=%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),nfd);
//子进程收发数据,父进程回收
if((pid=fork())==-1)
{
perror("fork error");
return -1;
}
if(pid>0)
{
close(nfd);
}
else if(pid==0)
{
close(sfd);
deal_msg(nfd,cin);
exit(EXIT_SUCCESS);
}
//5.接收发送数据
//6.关闭套接字文件
}
close(sfd);
return 0;
}
多线程服务器
#include
//tcp服务端
#define PORT 8888
#define IP "192.168.0.105"
struct pthread_ds
{
int nfd;
struct sockaddr_in cin;
};
void *deal_cli_msg(void *arg)
{
int nfd=((struct pthread_ds*)arg)->nfd;
struct sockaddr_in cin=((struct pthread_ds*)arg)->cin;
//5.接收发送数据
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
int res=recv(nfd,rbuf,sizeof(rbuf),0);
if(res==0)
{
printf("客户端下线\n");
break;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);
strcat(rbuf,":)");
send(nfd,rbuf,sizeof(rbuf),0);
}
close(nfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//1.创建套接字文件
int sfd=-1;
if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket error");
return -1;
}
//1.1端口号快速重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
}
//2.绑定ip地址,端口号
//2.1填充服务端地址信息结构体
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
//3.将套接字设置成监听状态
if(listen(sfd,128)==-1)
{
perror("listen error");
return -1;
}
//4.接受客户端连接请求
int nfd=-1;
struct sockaddr_in cin;
socklen_t socklen=sizeof(cin);
while(1)
{
if((nfd=accept(sfd,(struct sockaddr *)&cin,&socklen))==-1)
{
perror("accept error");
return -1;
}
pthread_t tid=-1;
struct pthread_ds info={nfd,cin};
if((pthread_create(&tid,NULL,deal_cli_msg,&info))!=0)
{
perror("pthread_create error");
return -1;
}
pthread_detach(tid);
}
//6.关闭套接字文件
close(sfd);
return 0;
}