tcp并发服务器代码

时间:2014-10-29 16:11:09
【文件属性】:

文件名称:tcp并发服务器代码

文件大小:4KB

文件格式:C

更新时间:2014-10-29 16:11:09

tcp

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 #include #include #include #include #include #include #include #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); }


网友评论

  • 好东西,可以参照一下
  • 代码挺详细的,容易看懂,很值得参考借鉴!经过调试可以运行!