多进程并发服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.31.206" //本机IP
typedef void (*sighandler_t)(int);
int rcv_cli_msg(int newfd,struct sockaddr_in cin);
void handler(int sig)
{
while(waitpid(-1,NULL,WNOHANG)>0);
}
int main(int argc, const char *argv[])
{
//捕获17号信号,用信号的方式回收僵尸进程
sighandler_t s=signal(SIGCHLD,handler);
if(SIG_ERR==s)
{
ERR_MSG("signal");
return -1;
}
//创建流式套接字
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
//允许端口快速重用
int reuse=1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用设置成功\n");
//填充IP和端口到地址信息结构体中,实际结构体是由地址族决定
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port =htons(8888);
sin.sin_addr.s_addr=inet_addr(IP);
//绑定服务器的IP地址和端口,必须绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听状态
if(listen(sfd,10)<0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
int newfd;
pid_t pid;
while(1)
{
//获取新的文件描述符,该文件描述符才是用于通信、交互的文件描述符
newfd=accept(sfd,NULL,NULL);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d] newfd=%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
pid=fork();
if(0==pid)
{
close(sfd);
rcv_cli_msg(newfd,cin);
close(newfd);
exit(0);
}
else if(pid>0)
{
close(newfd);
}
else
{
ERR_MSG("fork");
return -1;
}
}
close(newfd);
close(sfd);
return 0;
}
int rcv_cli_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)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
fprintf(stderr,"[%s:%d] newfd=%d客户端关闭\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
break;
}
printf("[%s:%d] newfd=%d : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
//发送
strcat(buf,"*_*");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
}
多线程并发服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.31.206" //本机IP
struct msg
{
int newfd;
struct sockaddr_in cin;
};
void* callBack(void* arg);
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
//允许端口快速重用
int reuse=1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用设置成功\n");
//填充IP和端口到地址信息结构体中,实际结构体是由地址族决定
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port =htons(8888);
sin.sin_addr.s_addr=inet_addr(IP);
//绑定服务器的IP地址和端口,必须绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听状态
if(listen(sfd,10)<0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
int newfd;
pthread_t tid;
struct msg info;
while(1)
{
//获取新的文件描述符,该文件描述符才是用于通信、交互的文件描述符
newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("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,(void*)&info)!=0)
{
ERR_MSG("pthread_create");
return -1;
}
pthread_detach(tid);
}
close(sfd);
return 0;
}
void* callBack(void* arg)
{
struct msg info=*(struct msg*)arg;
int newfd=info.newfd;
struct sockaddr_in cin=info.cin;
char buf[128]="";
ssize_t res=0;
while(1)
{
bzero(buf,sizeof(buf));
//接收
res=recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
break;
}
else if(0==res)
{
fprintf(stderr,"[%s:%d] newfd=%d客户端关闭\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
break;
}
printf("[%s:%d] newfd=%d : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
//发送
strcat(buf,"*_*");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
break;
}
printf("send success\n");
}
close(newfd);
pthread_exit(NULL);
}
流式域套接字
服务器>>>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/un.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd=socket(AF_UNIX,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
//判断要绑定的套接字文件路径是否存在
if(access("./unix",F_OK)==0)
{
if(unlink("./unix")<0)
{
ERR_MSG("unlink");
return -1;
}
}
//填充IP和端口到地址信息结构体中,实际结构体是由地址族决定
struct sockaddr_un sun;
sun.sun_family =AF_UNIX;
strcpy(sun.sun_path,"./unix");
//绑定服务器的IP地址和端口,必须绑定
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听状态
if(listen(sfd,10)<0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
//获取新的文件描述符,该文件描述符才是用于通信、交互的文件描述符
int newfd=accept(sfd,NULL,NULL);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("newfd=%d\n",newfd);
char buf[128]="";
ssize_t res=0;
while(1)
{
bzero(buf,sizeof(buf));
//接收
res=recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
fprintf(stderr,"newfd=%d客户端关闭\n",newfd);
break;
}
printf("newfd=%d : %s\n",newfd,buf);
//发送
strcat(buf,"*_*");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
close(newfd);
close(sfd);
return 0;
}
客户端>>>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/un.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd=socket(AF_UNIX,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
//填充要连接的服务器的IP和端口
struct sockaddr_un sun;
sun.sun_family =AF_UNIX;
strcpy(sun.sun_path,"./unix");
//连接服务器
if(connect(sfd,(struct sockaddr*)&sun,sizeof(sun))<0)
{
ERR_MSG("connect");
return -1;
}
char buf[128]="";
ssize_t res=0;
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
//发送
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
//接收
bzero(buf,sizeof(buf));
res=recv(sfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
fprintf(stderr,"服务器关闭\n");
break;
}
printf(": %s\n",buf);
}
close(sfd);
return 0;
}
报式域套接字
服务器>>>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/un.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
//判断要绑定的套接字文件路径是否存在
if(access("./unix",F_OK)==0)
{
if(unlink("./unix")<0)
{
ERR_MSG("unlink");
return -1;
}
}
//填充IP和端口到地址信息结构体中,实际结构体是由地址族决定
struct sockaddr_un sun;
sun.sun_family =AF_UNIX;
strcpy(sun.sun_path,"./unix1");
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
char buf[128]="";
ssize_t res=0;
struct sockaddr_un cun;
socklen_t addrlen=sizeof(cun);
while(1)
{
bzero(buf,sizeof(buf));
//接收
res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,&addrlen);
if(res<0)
{
ERR_MSG("recvfrom");
return -1;
}
else if(0==res)
{
fprintf(stderr,"newfd=%d客户端关闭\n",sfd);
break;
}
printf("[%s]:%s\n",cun.sun_path,buf);
//发送
strcat(buf,"*_*");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,sizeof(cun))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("send success\n");
}
close(sfd);
return 0;
}
客户端>>>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/un.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
if(access("./unix2",F_OK)==0)
{
if(unlink("./unix2")<0)
{
ERR_MSG("unlink");
return -1;
}
}
//绑定客户端自身的地址结构体--->必须绑定
struct sockaddr_un cun;
cun.sun_family =AF_UNIX;
strcpy(cun.sun_path,"./unix2");
if(bind(sfd,(struct sockaddr*)&cun,sizeof(cun))<0)
{
ERR_MSG("bind");
return -1;
}
struct sockaddr_un sun;
sun.sun_family =AF_UNIX;
strcpy(sun.sun_path,"./unix1");
char buf[128]="";
ssize_t res=0;
struct sockaddr_un rcv_addr;
socklen_t addrlen=sizeof(rcv_addr);
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
//发送
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sun,sizeof(sun))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
//接收
bzero(buf,sizeof(buf));
res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&rcv_addr,&addrlen);
if(res<0)
{
ERR_MSG("recvfrom");
return -1;
}
else if(0==res)
{
fprintf(stderr,"服务器关闭\n");
break;
}
printf("[%s]: %s\n",rcv_addr.sun_path,buf);
}
close(sfd);
return 0;
}