TCP并发服务器多进程编程

环境:linux C

功能:并发服务器实现cs通信

/*server.c*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h> //文件io read write close fork waitpid
#include <netinet/in.h>//socket() struct sockaddr_in  htonl htons 
#include <netinet/ip.h>//socket()
#include <arpa/inet.h>//inetaddr() inet_pton()
#include <signal.h>//signal
#define SERV_PORT 5001
#define SERV_IP_ADDR "192.168.152.128"
//子进程退出的消息处理函数
void sig_child_handler(int signo)
{
if(SIGCHLD == signo)
{
waitpid(-1,NULL,WNOHANG);
}
}
int main()
{
int fd = -1;
//创建socket
fd = socket(AF_INET,SOCK_STREAM,0);//流式套接字-TCP
if(fd == -1)
{
perror("socket");
exit(-1);
}
//2绑定socket
//2.1填充struct sockaddr_in
struct sockaddr_in sin;
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(SERV_PORT);//网络字节序的端口号
#if 1 
sin.sin_addr.s_addr = htonl(INADDR_ANY);//代替某个IP inet_addr(SERV_IP_ADDR) 让服务器能绑定在任意IP
#else
if(inet_pton(AF_INET,SERV_IP_ADDR,(void *)&sin.sin_addr.s_addr) != 1)
{
perror("inet_pton");
exit(-1);
}
#endif
//2.2开始绑定socket就是给fd带上属性
if(bind(fd,(const struct sockaddr *)&sin,sizeof(sin)) < 0)
{
perror("bind");
exit(-1);
}
//3 调用listen()将主动套接字变成被动套接字
if(listen(fd,5) == -1)
{
perror("listen");
exit(-1);
}
//4 多进程并发处理客户端连接请求 
while(1)
{
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);


int newfd = accept(fd,(struct sockaddr *)&cin,&addrlen);
if(newfd < 0 )
{
perror("accept");
break;
}
pid_t pid;
if((pid = fork()) < 0)
{
perror("fork");
break;
}
if(pid == 0)
{
close(fd);
char  ipv4_addr[16];
if(!inet_ntop(AF_INET,(void *)&cin.sin_addr.s_addr,ipv4_addr,sizeof(cin)))
{
perror("inet_ntop");
exit(-1);
}
printf("client(%s:%d) is connected\n",ipv4_addr,ntohs(cin.sin_port));
//5通过newfd进行cs通信
char buf[BUFSIZ];
bzero(buf,BUFSIZ);
int ret;
while(1)
{
while((ret = read(newfd,buf,BUFSIZ)) > 0)
{
if(strncasecmp(buf,"quit\n",4) == 0)
{
printf("client is exiting\n");
goto _exit;
}
printf("%s",buf);
}


if(ret == 0 )
{
break;
}
else
{
perror("read");
break;
}


}
exit(0);
}
close(newfd);
signal(SIGCHLD,sig_child_handler);
}
_exit:
//6关闭套接子
close(fd);
return 0;

}


/*client.c*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h> //文件io read write close
#include <netinet/in.h>//socket() struct sockaddr_in  htonl htons 
#include <netinet/ip.h>//socket()
#include <arpa/inet.h>//inetaddr() inet_pton()
#define SERV_PORT 5001
#define SERV_IP_ADDR "192.168.152.128"
int main()
{
int fd = -1;
//创建socket
fd = socket(AF_INET,SOCK_STREAM,0);//流式套接字-TCP
if(fd == -1)
{
perror("socket");
exit(-1);
}
//2 connect()申请连接
 //填充sockaddr_in()结构体
struct sockaddr_in sin;
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(SERV_PORT);//网络字节序的端口号
#if 0 
sin.sin_addr.s_addr = inet_addr(SERV_IP_ADDR);
#else
if(inet_pton(AF_INET,SERV_IP_ADDR,(void *)&sin.sin_addr.s_addr) != 1)
{
perror("inet_pton");
exit(-1);
}
#endif
//申请连接
if((connect(fd,(const struct sockaddr *)&sin,sizeof(sin))) < 0)
{
perror("connect");
exit(-1);
}
//
char buf[BUFSIZ];
while(1)
{
if(fgets(buf,BUFSIZ,stdin) == NULL)
{
continue;
}
write(fd,buf,BUFSIZ);
if(!strncasecmp(buf,"quit\n",4))
{
printf("client is exiting\n");
break;
}
}
//关闭套接字
close(fd);

return 0;
}

popen重写 /* * ===================================================================================== * * Filename: tcpserver.c * * Description: this program is demostrate to how to write a remote control server * * Version: 1.0 * Created: 2010年09月11日 21时28分21秒 * Revision: none * Compiler: gcc * * Author: Gang Liang (cs.scu.edu.cn/~lianggang), lianggang@scu.edu.cn * Company: Sichuan university * ===================================================================================== */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #define PORT 8900 #define BUFSIZE 2048 #define SHELL "/bin/sh" FILE *myPopen(char *command,char *type) { int f_des[2]; int pid_t; // pipe(f_des); if((type[0]!='r'&&type[0]!='w')||type[1]!=0) { printf("myPopen()flag error/n"); return NULL; } if(pipe(f_des)==-1) { printf("pipe create error/n"); return NULL; } pid_t=fork(); if(pid_t<0) return NULL; if(pid_t==0) { if(type[0]=='r') { close(f_des[0]); dup2(f_des[1],STDOUT_FILENO); close(f_des[1]); } else{ close(f_des[1]); dup2(f_des[0],STDIN_FILENO); close(f_des[0]); } execl(SHELL,"sh","-c",command,(char*)0); _exit(127); char *argv[] = {command,0}; if(execvp(command,argv)==-1) return NULL; } wait(0); if(type[0]=='r') { close(f_des[1]); return fdopen(f_des[0],"r"); } else{ close(f_des[0]); return fdopen(f_des[1],"w"); } } int execute(char*command,char*buf) { FILE *fp; int count; char commandbuf[2056]; if ((NULL==command)||(NULL==buf)) { perror("command or buf is empty\n"); return -1; } count =0; memset(commandbuf,0,2056); strcat(commandbuf,"sh -c "); strcat(commandbuf,command); fprintf(stderr,"the command is %s\n",commandbuf); if (NULL==(fp=myPopen(commandbuf,"r"))) { perror("create pipe error\n"); return -1; } while ((count<2047) && (EOF!=(buf[count++]=fgetc(fp)))); buf[count-1]='\0'; return count; } int main() { int sockfd; int conn_sock; char sendbuf[BUFSIZE]; char recvbuf[BUFSIZE]; int sendnum; int recvnum; int length; struct sockaddr_in client; struct sockaddr_in server; int opt; int cnt; pid_t pid; /* The first stage:INITILIZE */ memset(&client,0,sizeof(client)); memset(&server,0,sizeof(server)); memset(sendbuf,0,BUFSIZE); memset(recvbuf,0,BUFSIZE); length=0; sockfd=-1; conn_sock=-1; opt=SO_REUSEADDR; /*The second stage:create listen socket */ if (-1==(sockfd=socket(AF_INET,SOCK_STREAM,0))) { perror("create socket error\n"); return -1; } setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); /* The third stage:bind socket */ server.sin_family=AF_INET; server.sin_addr.s_addr=htonl(INADDR_ANY); server.sin_port=htons(PORT); if (-1==bind(sockfd,(struct sockaddr*)&server,sizeof(server))) { perror("bind socket error\n"); close(sockfd); return -1; } /* The fourth stage:listen socket */ if (-1==listen(sockfd,10)) { perror("listen socket error\n"); close(sockfd); return -1; } /* The fifth stage:creat connect socket */ while(1) { if (-1==(conn_sock=accept(sockfd,(struct sockaddr*)&client,&length))) { perror("three shakehands error\n"); close(sockfd); return -1; } /* the commnication with client */ pid=fork(); if(pid==0) { close(sockfd); while(1) { memset(recvbuf,0,BUFSIZE); memset(sendbuf,0,BUFSIZE); if (0>=(recvnum=read(conn_sock,recvbuf,BUFSIZE))) { perror("the commucation error\n"); close(conn_sock); close(sockfd); return -1; } recvbuf[recvnum]='\0'; fprintf(stderr,"the command is:%s\n",recvbuf); if (0==strcmp(recvbuf,"quit")) { fprintf(stderr,"the client is quit\n"); close(conn_sock); break; } if (1>=(cnt=execute(recvbuf,sendbuf))) { sprintf(sendbuf,"the invalid command,please try again\n"); } fprintf(stderr,"the result is \n%s",sendbuf); if (0>=(sendnum=write(conn_sock,sendbuf,strlen(sendbuf)))) { perror("the commucation error\n"); close(sockfd); close(conn_sock); return -1; } } } else if(pid>0) { close(conn_sock); continue; } } close(sockfd); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值