进程实现TCP并发服务器
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <string.h>
- #include <wait.h>
- #include <signal.h>
- #include <stdlib.h>
- #define ERR_MSG(Msg) do{\
- fprintf(stderr,"__%d__:",__LINE__);\
- perror(Msg);\
- }while(0)
- #define IP "192.168.8.10"
- #define PORT 6666
- typedef void (*sighandler_t)(int);
- int recvdata(int newsdpdp,struct sockaddr_in bin);
- void handler(int p)
- {
- while(waitpid(-1, NULL, WNOHANG) > 0);
- }
- int main(int argc, const char *argv[])
- {
- sighandler_t s = signal(SIGCHLD, handler);
- if(SIG_ERR == s)
- {
- ERR_MSG("signal");
- }
- //创建字节流式套接字
- int sdp=socket(AF_INET,SOCK_STREAM,0);
- if(sdp<0)
- {
- ERR_MSG("sdp");
- return -1;
- }
- //允许端口被重复使用
- int reuse = 1;
- if(setsockopt(sdp, 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(PORT);
- sin.sin_addr.s_addr=inet_addr(IP);
- socklen_t addrlen=sizeof(sin);
- if(bind(sdp,(struct sockaddr *)&sin,addrlen)<0)
- {
- ERR_MSG("bind");
- return -1;
- }
- printf("绑定成功\n");
- //将套接字设置为监听状态
- if(listen(sdp,128)<0)
- {
- ERR_MSG("listen");
- }
- printf("监听成功\n");
- struct sockaddr_in bin;
- socklen_t addrlen1=sizeof(bin);
- int newsdp=0;
- //生成新的文件描述符,存储链接成功的客户信息
- while(1)
- {
- newsdp=accept(sdp,(struct sockaddr *)&bin,&addrlen1);
- if(newsdp<0)
- {
- ERR_MSG("accept");
- return -1;
- }
- printf("对方客户端ip=[%s] port=%d连接成功 ",inet_ntoa(bin.sin_addr),ntohs(bin.sin_port));
- pid_t pid;
- pid=fork();
- if(pid<0)
- {
- ERR_MSG("fork");
- }
- if(pid>0)
- {
- close(newsdp);
- }
- if(pid==0)
- {
- close(sdp);
- recvdata(newsdp,bin);
- exit(0);
-
- }
- }
- close(sdp);
- return 0;
- }
- int recvdata(int newsdp,struct sockaddr_in bin)
- {
- //接受数据
- char buf[128];
- bzero(buf,sizeof(buf));
- while(1)
- {
- ssize_t rcv;
- rcv=recv(newsdp,buf,sizeof(buf),0);
- if(rcv<0)
- {
- ERR_MSG("recv");
- return -1;
- }
- if(rcv==0)
- {
- printf("对方客户端关闭\n");
- break;
- }
- printf("对方客户端ip=[%s] port=%d newsdp=%d读取到的数据为%s\n",inet_ntoa(bin.sin_addr),ntohs(bin.sin_port),newsdp,buf);
- //发送数据
- strcat(buf,"*-*");
- if(send(newsdp,buf,sizeof(buf),0)<0)
- {
- ERR_MSG("send");
- return -1;
- }
- printf("发送成功\n");
- }
- return 0;
- }
线程实现并发服务器
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <string.h>
- #include <wait.h>
- #include <signal.h>
- #include <stdlib.h>
- #include <pthread.h>
- #define ERR_MSG(Msg) do{\
- fprintf(stderr,"__%d__:",__LINE__);\
- perror(Msg);\
- }while(0)
- #define IP "192.168.8.10"
- #define PORT 6666
- void *callback(void *arg);
- struct msg
- {
- struct sockaddr_in bin;
- int newsdp;
- };
- int main(int argc, const char *argv[])
- {
- //创建字节流式套接字
- int sdp=socket(AF_INET,SOCK_STREAM,0);
- if(sdp<0)
- {
- ERR_MSG("sdp");
- return -1;
- }
- //允许端口被重复使用
- int reuse = 1;
- if(setsockopt(sdp, 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(PORT);
- sin.sin_addr.s_addr=inet_addr(IP);
- socklen_t addrlen=sizeof(sin);
- if(bind(sdp,(struct sockaddr *)&sin,addrlen)<0)
- {
- ERR_MSG("bind");
- return -1;
- }
- printf("绑定成功\n");
- //将套接字设置为监听状态
- if(listen(sdp,128)<0)
- {
- ERR_MSG("listen");
- }
- printf("监听成功\n");
- struct sockaddr_in bin;
- socklen_t addrlen1=sizeof(bin);
- int newsdp=0;
- pthread_t tid;
- //生成新的文件描述符,存储链接成功的客户信息
- while(1)
- {
- newsdp=accept(sdp,(struct sockaddr *)&bin,&addrlen1);
- if(newsdp<0)
- {
- ERR_MSG("accept");
- return -1;
- }
- printf("对方客户端ip=[%s] port=%d连接成功 ",inet_ntoa(bin.sin_addr),ntohs(bin.sin_port));
- struct msg sendmsg;
- sendmsg.newsdp=newsdp;
- sendmsg.bin=bin;
- if(pthread_create(&tid,NULL,callback,(void *)&sendmsg)<0)
- {
- ERR_MSG("pthread_create");
- }
- }
- pthread_detach(tid);
- close(sdp);
- return 0;
- }
- void *callback(void *arg)
- {
- //接受数据
- char buf[128];
- int newsdp=((struct msg *)arg)->newsdp;
- struct sockaddr_in bin=((struct msg *)arg)->bin;
- bzero(buf,sizeof(buf));
- while(1)
- {
- ssize_t rcv;
- rcv=recv(newsdp,buf,sizeof(buf),0);
- if(rcv<0)
- {
- ERR_MSG("recv");
- break;
- }
- if(rcv==0)
- {
- printf("对方客户端关闭\n");
- break;
- }
- printf("对方客户端ip=[%s] port=%d newsdp=%d读取到的数据为%s\n",inet_ntoa(bin.sin_addr),ntohs(bin.sin_port),newsdp,buf);
- //发送数据
- strcat(buf,"*-*");
- if(send(newsdp,buf,sizeof(buf),0)<0)
- {
- ERR_MSG("send");
- break;
- }
- printf("发送成功\n");
- }
- close(newsdp);
- return 0;
- }