多进程并发服务器
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.h>
#include<unistd.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 PORT 8888
#define IP "192.168.31.128"
typedef void (*sighandler_t)(int);
int rcv_cli_msg(int newfd,struct sockaddr_in cin)
{
char buf[128]="";
while(1)
{
bzero(buf,sizeof(buf));
size_t res=recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res ==0)
{
printf("[%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);
fgets(buf,sizeof(buf),stdin);
send(newfd,buf,sizeof(buf),0);
}
}
void handler(int sig)
{
while(waitpid(-1,NULL,WNOHANG)>0);
}
int main(int argc, const char *argv[])
{
sighandler_t s=signal(17,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;
}
printf("create socket success\n");
int reuse=1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("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);
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;
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);
pid_t pid=fork();
if(pid>0)
{
close(newfd);
}
else if(pid ==0)
{
close(sfd);
rcv_cli_msg(newfd,cin);
close(newfd);
exit(0);
}
else
{
ERR_MSG("fork");
return -1;
}
}
close(sfd);
close(newfd);
return 0;
}
多线程并发服务器
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/wait.h>
#include<pthread.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"_%d_",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.31.128"
struct msg
{
int newfd;
struct sockaddr_in cin;
};
void* _callback(void* arg)
{
pthread_detach(pthread_self());
int newfd= (*(struct msg*)arg).newfd;
struct sockaddr_in cin=(*(struct msg*)arg).cin;
char buf[128]="";
while(1)
{
bzero(buf,sizeof(buf));
size_t res=recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return NULL;
}
else if(res ==0)
{
printf("[%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);
fgets(buf,sizeof(buf),stdin);
send(newfd,buf,sizeof(buf),0);
}
close(newfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("create socket success\n");
int reuse=1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("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);
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;
struct msg cliinfo;
pthread_t tid;
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);
cliinfo.newfd=newfd;
cliinfo.cin=cin;
if(pthread_create(&tid,NULL,_callback,(void*)&cliinfo)!=0)
{
ERR_MSG("pthread_create");
return -1;
}
}
close(sfd);
close(newfd);
return 0;
}
域套接字tcp
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.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_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("create socket success\n");
if(access("./unix",F_OK)==0)
{
if(unlink("./unix")<0)
{
ERR_MSG("unlink");
}
}
struct sockaddr_un sun;
sun.sun_family =AF_UNIX;
strcpy(sun.sun_path,"./unix");
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");
struct sockaddr_in cin;
socklen_t addrlen =sizeof(cin);
int newfd =accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("newfd=%d\n",newfd);
char buf[128]="";
while(1)
{
bzero(buf,sizeof(buf));
size_t res=recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res ==0)
{
printf("newfd=%d读取失败\n",newfd);
break;
}
printf(" newfd=%d %s\n",newfd,buf);
fgets(buf,sizeof(buf),stdin);
send(newfd,buf,sizeof(buf),0);
}
close(sfd);
close(newfd);
return 0;
}
域套接字udp
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.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("./udp_unix",F_OK)==0)
{
if(unlink("./udp_unix")<0)
{
ERR_MSG("unlink");
return -1;
}
}
struct sockaddr_un sun;
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path,"udp_unix");
if(bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
struct sockaddr_un cin;
socklen_t addrlen = sizeof(cin);
char buf[128] = "";
while(1)
{
bzero(buf, sizeof(buf));
if(recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen) < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("%s:%s\n", sun.sun_path,buf);
bzero(buf, sizeof(buf));
printf("请输入>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,addrlen)<0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
}
close(sfd);
return 0;
}