LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: stoat

linux socket 通信中并发服务器拒绝连接,重置连接,客户端自动关闭问题

[复制链接]
 楼主| 发表于 2007-1-23 21:27:07 | 显示全部楼层
我添加了信号处理及io复用后,测试结果是信号确实会产生,然后程序终止,但是按要求产生信号后,只需提示一下即可,然后程序继续运行,我不知道这个处理函数怎么写,好象handler怎么写也会不到upload函数中让它继续运行呀(现在是捕捉到信号后,不停地打印handler中的printf,然后终止了):

  1. 1  void handler(){printf("error caused by signal\n");}
  2. 2  int upload(){
  3. 3      int f;//文件标识符
  4. 4      size_t rsize;//读取长度
  5. 5      struct stat buffer;//结构,用来取得文件大小
  6. 6      unsigned char buf[1024+1];//缓冲区
  7. 7      struct sockaddr_in servaddr;      
  8. 8      int ret;
  9. 9      struct timeval timeout;
  10. 10    fd_set rfd,wfd,efd;
  11. 11    struct sigaction newact;
  12.        //初始化套接口,hostname和port是经过参数初始化了的服务器ip和端口
  13. 12    servaddr.sin_family=AF_INET:
  14. 13    servaddr.sin_port=htons(port);
  15. 14    if(inet_pton(AF_INET,hostname,&servaddr.sin_addr)<=0){
  16. 15         perror("inet_pton");
  17. 16    }
  18. 17    fcntl(sockfd,F_SETFD,O_NONBLOCK);
  19. 18    timeout.tv_sec=2;
  20. 19    timeout.tv_usec=0;
  21. 20    newact.sa_flags=0;
  22. 21    sigemptyset(&newact.sa_mask);  
  23. 22    sigaddset(&newact.sa_mask,SIGPIPE);
  24. 23    sigaddset(&newact.sa_mask,SIGSEGV);
  25. 24    if(sigaction(SIGPIPE,&newact,NULL)<0){
  26. 25         printf("sigsegv output info\n");
  27. 26    }
  28. 27    if(sigaction(SIGSEGV,&newact,NULL)<0){
  29. 28          printf("sigpipe output info\n");
  30. 29   }
  31. 30   while(1){
  32. 31         stat(filename,&buffer);//取得文件属性
  33.            //打开文件
  34. 32         if((f=open("filename",O_RDONLY))<0){
  35. 33             perror("can't open file");
  36. 34             continue;
  37. 35         }
  38.            //生成套接口
  39. 36         if(sockfd=socket(AF_INET,SOCK_STREAM,0)<0){
  40. 37             perror("socket");
  41. 38             close(f);
  42. 39             continue;
  43. 40         }
  44.            //连接服务器
  45. 41         if(connect(sockfd,(struct sockaddr *)&seraddr,sizeof(struct sockaddr))<0)        {
  46. 42             perror("connect");
  47. 43             close(f);
  48. 44             close(sockfd);
  49. 45             continue;
  50. 46         }
  51. 47         resize=buffer.st_size;//文件大小           
  52. 48         FD_ZERO(&rfd);
  53. 49         FD_ZERO(&wfd);
  54. 50         FD_ZERO(&efd);
  55. 51         FD_SET(f,&rfd);
  56. 52         FD_SET(sockfd,&wfd);
  57. 53         FD_SET(sockfd,&efd);
  58. 54         ret=select(sockfd+1,&rfd,&wfd,&efd,&timeout);
  59. 55         if(ret==0)
  60. 56             continue;
  61. 57         if(ret<0){
  62. 58             perror("select error:");
  63. 59             exit(-1);
  64. 60         }
  65. 61         alarm(3);
  66. 62         if(FD_ISSET(f,&rfd)&&FD_ISSET(sockfd,&wfd)){
  67. 63             while(rsize>0){
  68. 64                 resize -=readn(f,buf,1024);//readn()函数是经过封装了read函数能准确读一定字节文件的函数
  69. 65                 writen(sockfd,buf,1024);//和readn函数一样封装过write函数
  70. 66             }
  71. 67         }
  72. 68         alarm(0);
  73. 69         close(f);
  74. 70         close(sockfd);
  75. 71     }
  76. 72 }
复制代码
回复 支持 反对

使用道具 举报

发表于 2007-1-24 20:42:13 | 显示全部楼层
你的newact好像没设置处理函数吧
另外你的handler也不符合处理函数的原型
man sigaction 看看
  1. #include <signal.h>
  2. int sigaction(int signum, const struct sigaction *act, struct sigaction
  3.        *oldact);
  4. struct sigaction {
  5.     void (*sa_handler)(int);
  6.     void (*sa_sigaction)(int, siginfo_t *, void *);
  7.     sigset_t sa_mask;
  8.     int sa_flags;
  9.     void (*sa_restorer)(void);
  10. };
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-1-26 11:40:06 | 显示全部楼层
问题解决了,在产生信号之前下工夫,当受到fin时continue
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表