|
|
一个简单的select学习 但是在服务端或者客户端断开的时候
另一个就会不断打印 上次接受到的内容 本人很菜 怎么也看不出来问题
还请高手指教
- server.c
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <sys/msg.h>
- #include <unistd.h>
- #include <time.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/msg.h>
- #include <signal.h>
- #include <sys/uio.h>
- #include <sys/ipc.h>
- #include <netdb.h>
- #include <sys/wait.h>
- #include <sys/time.h>
- void SetExitFlag(int signo);
- int flag=1;
- int main(){
-
- struct sockaddr_in servaddr,cliaddr;
- struct hostent *lhost;
- int sfd;
- int reuseaddr = 1;
- int recvfd;
- int clilen=sizeof(struct sockaddr_in);
- int recvlen;
- char buf[1024];
- int sendlen;
- struct sigaction act,oact;
- uint16_t port;
- char addr[17];
- fd_set readset,writeset;
-
- memset(buf,0,sizeof(buf));
- memset(addr,0,sizeof(addr));
-
- /**信号设置**/
- act.sa_handler=SetExitFlag;
- sigemptyset(&act.sa_mask);
- act.sa_flags=0;
- if(sigaction(SIGINT,&act,&oact)!=0)
- {
- perror("sigaction");
- exit(1);
- }
- signal(SIGTERM, SetExitFlag);
- signal(SIGKILL, SetExitFlag);
- signal(SIGSTOP, SetExitFlag);
- signal(SIGQUIT, SetExitFlag);
-
- /*lhost=gethostbyaddr("dctest1");
- printf("主机名:[%s][%s]\n",lhost->h_name,lhost->h_addr);*/
-
- sfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sfd<0)
- perror("socket ");
- if(setsockopt(sfd,SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr)) == -1)
- perror("reuse addr ");
- if(setsockopt(sfd,SOL_SOCKET, SO_REUSEPORT, &reuseaddr, sizeof(reuseaddr)) == -1)
- perror("reuse port");
-
- bzero((char *)&servaddr,sizeof(servaddr));
- servaddr.sin_family=AF_INET;
- servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
- servaddr.sin_port=htons(10006);
-
- if(bind(sfd,(struct sockaddr *)&servaddr,sizeof(struct sockaddr_in)) == -1)
- perror("bind ");
-
- if(listen(sfd,10) == -1)
- perror("listen ");
-
-
- while(flag)
- {
-
- if((recvfd=accept(sfd,(struct sockaddr *)&cliaddr,&clilen)) == -1)
- {
- perror("accept ");
- break;
- }
- port=ntohs(cliaddr.sin_port);
- strcpy(addr,inet_ntoa(cliaddr.sin_addr));
- printf("接收到连接 端口:[%d]ip[%s]\n",port,addr);
- /*FD_ZERO(&readset);
- FD_SET(0,&readset);
- FD_SET(recvfd,&readset);
- if(select(recvfd+1,&readset,NULL,NULL,NULL)<0)
- perror("select ");*/
- while(flag)
- {
- FD_ZERO(&readset);
- FD_SET(0,&readset);
- FD_SET(recvfd,&readset);
- if(select(recvfd+1,&readset,NULL,NULL,NULL)<0)
- perror("select ");
- if(FD_ISSET(0,&readset))
- {
- gets(buf);
- sendlen=write(recvfd,buf,strlen(buf));
- if(sendlen < 0 )
- {
- perror("send ");
- close(recvfd);
- break;
- }
- }
- if(FD_ISSET(recvfd,&readset))
- {
- recvlen=read(recvfd,buf,1024);
- if(recvlen < 0)
- {
- perror("recv ");
- close(recvfd);
- break;
- }
- else
- printf("recv:[%s]\n",buf);
- }
-
- }
- close(recvfd);
- }
- }
- void SetExitFlag(int signo)
- {
- flag=0;
- printf("接收到退出信号\n");
- }
- client.c
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <sys/msg.h>
- #include <unistd.h>
- #include <time.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/msg.h>
- #include <signal.h>
- #include <sys/uio.h>
- #include <sys/ipc.h>
- #include <netdb.h>
- #include <sys/wait.h>
- #include <sys/time.h>
- void SetExitFlag(int signo);
- int flag=1;
- int main(){
-
- struct sockaddr_in conaddr;
- int cfd;
- char buf[1024];
- int sendlen;
- int recvlen;
- fd_set readset,writeset;
- struct sigaction act,oact;
-
- memset(buf,0,1024);
- /**信号设置**/
- act.sa_handler=SetExitFlag;
- sigemptyset(&act.sa_mask);
- act.sa_flags=0;
- if(sigaction(SIGINT,&act,&oact)!=0)
- {
- perror("sigaction");
- exit(1);
- }
- signal(SIGTERM, SetExitFlag);
- signal(SIGKILL, SetExitFlag);
- signal(SIGSTOP, SetExitFlag);
- signal(SIGQUIT, SetExitFlag);
-
- cfd=socket(AF_INET,SOCK_STREAM,0);
- if(cfd == -1)
- perror("socket ");
-
- bzero((char *)&conaddr,sizeof(conaddr));
-
- conaddr.sin_family=AF_INET;
- conaddr.sin_addr.s_addr=inet_addr("172.16.227.11");
- conaddr.sin_port=htons(10006);
-
- if(connect(cfd,(struct sockaddr *)&conaddr,sizeof(struct sockaddr_in)) == -1)
- {
- perror("bind ");
- close(cfd);
- exit(1);
- }
-
- /*FD_ZERO(&readset);
- FD_SET(0,&readset);
- FD_SET(cfd,&readset);
- if(select(cfd+1,&readset,NULL,NULL,NULL)<0)
- perror("select ");*/
- while(flag)
- {
- FD_ZERO(&readset);
- FD_SET(0,&readset);
- FD_SET(cfd,&readset);
- if(select(cfd+1,&readset,NULL,NULL,NULL)<0)
- perror("select ");
- memset(buf,0,sizeof(buf));
- if(FD_ISSET(0,&readset))
- {
- gets(buf);
- sendlen=write(cfd,buf,sizeof(buf));
- if(sendlen<0)
- {
- perror("send ");
- break;
- }
- }
- if(FD_ISSET(cfd,&readset))
- {
- recvlen=read(cfd,buf,1024);
- if(recvlen<0)
- {
- perror("recv ");
- break;
- }
- else
- printf("接受:[%s]\n",buf);
- }
- }
-
- close(cfd);
-
- }
- void SetExitFlag(int signo)
- {
- flag=0;
- printf("接收到退出信号\n");
- }
复制代码 |
|