TCP多进程并发服务器
#include <stdio.h>
#include "/home/ubuntu/head.h"#define IP "192.168.124.85"
#define PORT 8888void handler(int sig)
{while(waitpid(-1,NULL,WNOHANG)>0);
}int main(int argc, const char *argv[])
{//回收僵尸进程if(signal(17,handler) == SIG_ERR){ERR_MSG("signal");return -1;}//创建流式套接字int sfd=socket(AF_INET,SOCK_STREAM,0);if(sfd<0){ERR_MSG("socket");return -1;}printf("socket create success sfd=%d __%d__\n",sfd,__LINE__);//允许端口快速被复用---监测端口号是否真的有进程在占用,如果没有,则快速复用int reuse = 1;if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0){ERR_MSG("setsockopt");return -1;}printf("允许端口快速复用成功\n");//定义并填充地址信息结构体,给服务绑定使用//真实的地址信息结构体根据地址族指定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("binf");return -1;}printf("bind success __%d__\n",__LINE__);//讲套接字设置为被动接听状态if(listen(sfd,128)<0){ERR_MSG("listen");return -1;}printf("listen success __%d__\n",__LINE__);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 客服端连接成功 __%d__\n",newfd,__LINE__);//创建父子进程pid_t cpid = fork();if(cpid<0){ERR_MSG("recv");return -1;}else if(0 == cpid){char buf[128]="";ssize_t res;while(1){//接受客服端数据res = recv(newfd,buf,sizeof(buf),0);if(res<0){ERR_MSG("recv");return -1;}else if(0 == res){printf("[%s : %d]newfd=%d 客服端下线 __%d__\n",\inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);break;}printf("[%s : %d]newfd=%d : %s __%d__\n",\inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf,__LINE__);//向客户端发送数据strcat(buf,"^_^");if(send(newfd,buf,sizeof(buf),0)<0){ERR_MSG("send");return -1;}printf("send success __%d__\n",__LINE__);}exit(0);}//父进程close(newfd);}return 0;
}
TCP多线程并发服务器
#include <stdio.h>
#include "/home/ubuntu/head.h"#define IP "192.168.124.85"
#define PORT 8888struct climsg
{int newfd;struct sockaddr_in cin;
};void* deal_cli_msg(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;}printf("socket create success sfd=%d __%d__\n",sfd,__LINE__);//允许端口快速被复用---监测端口号是否真的有进程在占用,如果没有,则快速复用int reuse = 1;if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0){ERR_MSG("setsockopt");return -1;}printf("允许端口快速复用成功\n");//定义并填充地址信息结构体,给服务绑定使用//真实的地址信息结构体根据地址族指定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("binf");return -1;}printf("bind success __%d__\n",__LINE__);//讲套接字设置为被动接听状态if(listen(sfd,128)<0){ERR_MSG("listen");return -1;}printf("listen success __%d__\n",__LINE__);struct sockaddr_in cin;socklen_t addrlen = sizeof(cin);pthread_t tid;struct climsg info;while(1){info.newfd = accept(sfd, (struct sockaddr*)&info.cin, &addrlen);if(info.newfd < 0){ERR_MSG("accept");return -1;}if(pthread_create(&tid,NULL,deal_cli_msg,(void*)&info)!=0){fprintf(stderr,"pthread_create _%d_\n", __LINE__);return -1;}pthread_detach(tid);}//关闭if(close(sfd) < 0){ERR_MSG("close");return -1;}return 0;
}void* deal_cli_msg(void* arg) //void* arg = &info
{int newfd = ((struct climsg*)arg)->newfd;struct sockaddr_in cin = ((struct climsg*)arg)->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){printf("[%s : %d] newfd=%d 客户端下线 __%d__\n", \inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);break;}printf("[%s : %d] newfd=%d : %s __%d__\n", \inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf, __LINE__);//向客户端发送数据---->//从终端获取数据strcat(buf, "^_^");if(send(newfd, buf, sizeof(buf), 0) < 0){ERR_MSG("send");break;}printf("send success __%d__\n", __LINE__);} close(newfd);printf("newfd = %d被关闭\n", newfd);pthread_exit(NULL);
}