多进程实现并发服务器
#include <myhead.h>
#define SER_IP "192.168.38.128"
#define SER_PORT 8888
void sighandler(int sig)
{
if(sig==SIGCHLD)
{
while(waitpid(-1,NULL,WNOHANG)>0);
}
}
int main(int argc, const char *argv[])
{
signal(SIGCHLD,sighandler);
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)return -1;
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(SER_PORT);
sin.sin_addr.s_addr=inet_addr(SER_IP);
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind\n");
return -1;
}
if(listen(sfd,128)==-1)
{
perror("listen\n");
return -1;
}
struct sockaddr_in apt;
socklen_t len=sizeof(apt);
while(1)
{
int newfd = accept(sfd,(struct sockaddr*)&apt,&len);
if(newfd == -1)
{
perror("newfd\n");
return -1;
}
printf("[%s:%d]:已连接\n",\
inet_ntoa(apt.sin_addr),ntohs(apt.sin_port));
int res=fork();
if(res > 0)
{
close(newfd);
}
else if(res == 0)
{
close(sfd);
char buf[256];
while(1)
{
bzero(buf,sizeof(buf));
if(read(newfd,buf,sizeof(buf))==0)
{
printf("客户端已下线\n");
break;
}
printf("客户端消息:%s\n",buf);
char a[20]={"receive success\n"};
write(newfd,a,sizeof(a));
}
close(newfd);
exit(0);
}
}
close(sfd);
return 0;
}
多线程实现并发服务器
#include <myhead.h>
#define SER_IP "192.168.38.128"
#define SER_PORT 8888
int newfd;
//线程函数,用于接收客户端消息
void* run(void* arg)
{
int nefd=newfd;
char buf[256];
while(1)
{
bzero(buf,sizeof(buf));
if(read(nefd,buf,sizeof(buf))==0)
{
printf("客户端已下线\n");
break;
}
printf("客户端消息:%s\n",buf);
char a[20]={"receive success\n"};
write(nefd,a,sizeof(a));
}
close(nefd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建套接字
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)return -1;
//绑定服务器
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(SER_PORT);
sin.sin_addr.s_addr=inet_addr(SER_IP);
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind\n");
return -1;
}
//设置监听
if(listen(sfd,128)==-1)
{
perror("listen\n");
return -1;
}
//填充结构体,用于接收客户端信息
struct sockaddr_in apt;
socklen_t len=sizeof(apt);
while(1)
{
//阻塞等待客户端连接
newfd = accept(sfd,(struct sockaddr*)&apt,&len);
if(newfd == -1)
{
perror("newfd\n");
return -1;
}
printf("[%s:%d]:已连接\n",\
inet_ntoa(apt.sin_addr),ntohs(apt.sin_port));
//创建线程
pthread_t id;
pthread_create(&id,NULL,run,NULL);
//回收线程资源
pthread_detach(id);
}
close(sfd);
return 0;
}
流式域套接字的实现
服务器端
#include <myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_UNIX,SOCK_STREAM,0);
if(sfd==-1)
{
perror("socket");
return -1;
}
//判断套接字文件是否存在,存在则删除
if(access("./unix",F_OK)==0)
{
unlink("./unix");
}
//填充结构体,绑定套接字文件
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))==-1)
{
perror("bind");
return -1;
}
//设置监听
if(listen(sfd,128)==-1)
{
perror("listen");
return -1;
}
//填充结构体,接收客户端信息
struct sockaddr_un cun;
cun.sun_family=AF_UNIX;
socklen_t len=sizeof(cun);
//阻塞等待客户端连接
int newfd=accept(sfd,(struct sockaddr*)&cun,&len);
if(newfd==-1)
{
perror("newfd");
return -1;
}
//数据的收发
char buf[128];
while(1)
{
bzero(buf,128);
int re=read(newfd,buf,128);
if(re == 0)
{
printf("客户端已下线\n");
break;
}
printf("%s\n",buf);
char a[]="receive success";
write(newfd,a,sizeof(a));
}
close(newfd);
close(sfd);
return 0;
}
客户端
#include <myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int cfd=socket(AF_UNIX,SOCK_STREAM,0);
if(cfd==-1)
{
perror("socket");
return -1;
}
/*
//判断套接字文件是否存在,存在则删除
if(access("./lnix",F_OK)==0)
{
unlink("./lnix");
}
//填充结构体,绑定套接字文件
struct sockaddr_un cun;
cun.sun_family=AF_UNIX;
strcpy(cun.sun_path,"./lnix");
if(bind(cfd,(struct sockaddr*)&cun,sizeof(cun))==-1)
{
perror("bind");
return -1;
}
*/
//填充结构体,用于连接服务端
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
if(connect(cfd,(struct sockaddr*)&sun,sizeof(sun))==-1)
{
perror("connect");
return -1;
}
//数据的收发
char buf[128];
while(1)
{
printf("请输入:");
scanf("%s",buf);
if(strcmp(buf,"quit")==0)
{
break;
}
write(cfd,buf,128);
bzero(buf,128);
read(cfd,buf,128);
printf("服务器发来消息:%s\n",buf);
}
close(cfd);
return 0;
}
报式域套接字实现
服务器端
#include <myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(sfd==-1)
{
perror("sfd");
return -1;
}
//判断套接字文件是否存在,存在则删除
if(access("./unix",F_OK)==0)
{
unlink("./unix");
}
//填充结构体,绑定服务端信息
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))==-1)
{
perror("bind");
return -1;
}
//填充结构体,用于向客户端发送消息(前提是客户端使用bind绑定了自己的信息)
//struct sockaddr_un cun;
//cun....
//数据的收发
char buf[128];
while(1)
{
//收
bzero(buf,128);
read(sfd,buf,128);
printf("%s\n",buf);
//发
}
close(sfd);
return 0;
}
客户端
#include <myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int cfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(cfd==-1)
{
perror("cfd");
return -1;
}
/*
//判断套接字文件是否存在,存在则删除
if(access("./lnix",F_OK)==0)
{
unlink("./lnix");
}
//填充结构体,绑定服务端信息
struct sockaddr_un cun;
cun.sun_family=AF_UNIX;
strcpy(cun.sun_path,"./lnix");
if(bind(cfd,(struct sockaddr*)&cun,sizeof(cun))==-1)
{
perror("bind");
return -1;
}
*/
//填充服务端结构体,用于向指定服务端发送消息
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
//数据的收发
char buf[128];
while(1)
{
//发
printf("请输入:");
scanf("%s",buf);
sendto(cfd,buf,128,0,(struct sockaddr*)&sun,sizeof(sun));
/*收
bzero(buf,128);
read(cfd,buf,128);
printf("%s\n",buf);
*/
}
close(cfd);
return 0;
}