多进程并发服务器
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/wait.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"_%d_:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 6666
#define IP "192.168.31.57"
typedef void (*sighandler_t)(int);
int rcv_cli_mag(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;
}
//绑定服务器的IP和端口
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))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//设置为被动监听状态
if(listen(sfd,128)<0)
{
ERR_MSG("listen");
return -1;
}
struct sockaddr_in cin;
//获取新的文件描述符
socklen_t addrlen=sizeof(cin);
while(1)
{
int newfd = accept (sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd <0)
{
ERR_MSG("accept");
return -1;
}
printf("newfd = %d\n",newfd);
pid_t pid = fork();
if(0==pid)
{
close(sfd);
rcv_cli_mag(newfd,cin);
close(newfd);
exit(0);
}
else if(pid>0)
{
close(newfd);
}
else
{
ERR_MSG("fork");
return -1;
}
}
close(sfd);
return 0;
}
int rcv_cli_mag(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,"%d客户端关闭\n",newfd);
break;
}
printf("%s",buf);
//发送
strcat(buf,"*****");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
}
return 0;
}
多线程并发服务器
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/wait.h>
#include<pthread.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"_%d_:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 6666
#define IP "192.168.31.57"
struct msg
{
int newfd;
struct sockaddr_in cin;
};
void *callBack_front(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;
}
//绑定服务器的IP和端口
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))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//设置为被动监听状态
if(listen(sfd,128)<0)
{
ERR_MSG("listen");
return -1;
}
struct sockaddr_in cin;
//获取新的文件描述符
socklen_t addrlen=sizeof(cin);
struct msg info;
// info.newfd = newfd;
info.cin=cin;
while(1)
{
int newfd = accept (sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd <0)
{
ERR_MSG("accept");
return -1;
}
printf("newfd = %d\n",newfd);
pthread_t tid1;
info.newfd = newfd;
// info.cin=cin;
if(pthread_create(&tid1,NULL,callBack_front,(void*)&info)!=0)
{
ERR_MSG("pthread_creat");
return -1;
}
pthread_detach(tid1);
}
close(sfd);
return 0;
}
void *callBack_front(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,"%d客户端关闭\n",newfd);
break;
}
printf("%s\n",buf);
//发送
strcat(buf,"*****");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
break;
}
}
close(newfd);
pthread_exit(NULL);
}
域式套接字客户端——UDP
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/socket.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.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;
}
printf("bind success\n");
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;
}
bzero(buf,sizeof(buf));
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&rcv_addr,&addrlen);
if(res <0)
{
ERR_MSG("recvfrom");
return -1;
}
}
close(sfd);
return 0;
}
域式套接字服务端——UDP
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/socket.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.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("./unix1",F_OK)==0)
{
if(unlink("./unix1")<0)
{
ERR_MSG("unlink");
return -1;
}
}
struct sockaddr_un cun;
cun.sun_family = AF_UNIX;
strcpy(cun.sun_path,"./unix1");
if(bind(sfd,(struct sockaddr*)&cun,sizeof(cun))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
char buf[128]="";
ssize_t res = 0;
struct sockaddr_un rcv_addr;
socklen_t addrlen = sizeof(rcv_addr);
while(1)
{
bzero(buf,sizeof(buf));
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&rcv_addr,&addrlen);
if(res <0)
{
ERR_MSG("recvfrom");
return -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*)&rcv_addr,sizeof(rcv_addr))<0)
{
ERR_MSG("sendto");
return -1;
}
}
close(sfd);
return 0;
}
域式套接字服务器——TCP
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<arpa/inet.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.31.57"
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(nulink("./unix")<0)
{
ERR_MSG("unlink");
return -1;
}
}
//绑定服务器的IP地址和端口号
struct sockaddr_un sun;
sun.sun_family = AF_INET;
strcpy(sun.sun_path,"./unix");
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))<0)
{
ERR_MSG("bind");
return -1;
}
//将套接字设置为被动监听设备
if(listen(sfd,10)<0)
{
ERR_MSG("listen");
return -1;
}
//获取连接成功后的套接字
struct sockaddr_un cun;
socklen_t addrlen = sizeof(cun);
int newfd=accept(sfd,(struct sockaddr*)&cun,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
//接收客户端的信息
char buf[128]="";
while(1)
{
bzero(buf,sizeof(buf));
ssize_t res = recv(newfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
{
printf("客户端关闭\n");
return -1;
}
printf("发送成功\n");
//发送信息给客户端
strcat(buf,"**");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
close(newfd);
close(sfd);
return 0;
}
域式套接字客户端——TCP
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<arpa/inet.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.31.57"
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_INET;
strcpy(sun.sun_path,"./unix");
if(connect(sfd,(struct sockaddr*)&sun,sizeof(sun))<0)
{
ERR_MSG("connect");
return -1;
}
//接收客户端的信息
char buf[128]="";
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入");
fget(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
ssize_t res = recv(newfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
{
printf("服务器关闭\n");
return -1;
}
printf("发送成功\n");
}
close(newfd);
close(sfd);
return 0;
}