1.多进程服务器
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<strings.h>
#include<signal.h>
#include<sys/wait.h>
#define PROT 6666
#define IP "192.168.10.136"
int del_cin_msg(int newfd,struct sockaddr_in cin);
void handel(int sig)
{
while(waitpid(-1,NULL,WNOHANG)>0);
}
int main(int argc, const char *argv[])
{
if(signal(17,handel) ==SIG_ERR)
{
perror("signal");
return -1;
}
//创建套接字
int fds = socket(AF_INET,SOCK_STREAM,0);
if(fds < 0)
{
perror("socket");
return -1;
}
//允许端口快速复用
int reuse =1;
if(setsockopt(fds,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
perror("setsockopt");
return -1;
}
printf("socket success\n");
//定义服务器结构体信息
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PROT);
sin.sin_addr.s_addr = inet_addr(IP);
//绑定bind
if(bind(fds,(struct sockaddr *)&sin,sizeof(sin))<0)
{
perror("bind");
return -1;
}
printf("bind success\n");
//listen 队列
if(listen(fds,128)<0)
{
perror("listen");
return -1;
}
struct sockaddr_in cin;
socklen_t len =sizeof(cin);
int newfd;
while(1)
{
newfd = accept(fds,(struct sockaddr *)&cin,&len);
if(newfd<0)
{
perror("accept");
return -1;
}
printf("[%s:%d]客户端连接成功\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
if(0 == fork())
{
close(fds);
del_cin_msg(newfd,cin);
exit(0);
}
//三种情况怎么解决
if(close(newfd)<0)
{
perror("close");
return -1;
}
}
//关闭
if(close(fds)<0)
{
perror("close");
return -1;
}
return 0;
}
int del_cin_msg(int newfd,struct sockaddr_in cin )
{
//接受信息
char buf[128]="";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
res = recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
perror("recv");
return -1;
}
else if(0 == res)
{
printf("[%s:%d]客户端下线newfd = %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
break;
}
else
{
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
}
//发送信息
strcat(buf,"*-*");
send(newfd,buf,sizeof(buf),0);
}
close(newfd);
}
结果:
多线程服务器
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#define PORT 6666
#define IP "192.168.10.136"
struct msg
{
int newfd;
struct sockaddr_in cin;
};
void *callback(void *arg)
{
int newfd = ((struct msg *)arg)->newfd;
struct sockaddr_in cin = ((struct msg *)arg)->cin;
char buf[128]="";
ssize_t res =0;
while(1)
{
bzero(buf,sizeof(buf));
res = recv(newfd,buf,sizeof(buf),0);
if(res < 0)
{
perror("recv");
return NULL;
}
else if(0 == res)
{
printf("[%s:%d]客户端已下线newfd = %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
break;
}
else
{
printf("[%s:%d]newfd=%d :%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
}
//发送信息
strcat(buf,"*-*");
send(newfd,buf,sizeof(buf),0);
}
close(newfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建套接字
int fds = socket(AF_INET,SOCK_STREAM,0);
if(fds < 0)
{
perror("fds");
return -1;
}
printf("socket success\n");
//允许端口快速复用
int reuse = 1;
if(setsockopt(fds,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
perror("setsockopt");
return -1;
}
//绑定服务器结构体信息
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//绑定bind
if(bind(fds,(struct sockaddr *)&sin,sizeof(sin))<0)
{
perror("bind");
return -1;
}
printf("bind success\n");
//listen
if(listen(fds,128)<0)
{
perror("listen");
return -1;
}
struct sockaddr_in cin;
int newfd;
socklen_t len =sizeof(cin);
pthread_t tid;
struct msg info;
while(1)
{
newfd = accept(fds,(struct sockaddr *)&cin,&len);
if(newfd < 0)
{
perror("accept");
return -1;
}
printf("[%s:%d]客户端连接成功newfd =%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
info.newfd = newfd;
info.cin =cin;
//创建线程
if(pthread_create(&tid,NULL,callback,&info)!=0)
{
fprintf(stdout,"pthread_create failed\n");
close(newfd);
break;
}
pthread_detach(tid);
}
//关闭
if(close(fds)<0)
{
perror("close");
return -1;
}
return 0;
}
结果: