谷动谷力

 找回密码
 立即注册
查看: 1546|回复: 0
打印 上一主题 下一主题
收起左侧

Linux TCPIP server报Socket accept: Invalid argument再次运行报端口占用

[复制链接]
跳转到指定楼层
楼主
发表于 2021-9-7 00:04:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 sunsili 于 2021-12-17 15:00 编辑

Linux TCPIP server报Socket accept: Invalid argument再次运行报端口占用

问题现象:
            server(服务端)运行在accept函数处等待client的连接,当有client来连接时,accept函数返回值为-1(错误).
   报错 Socket accept: Invalid argument
           问题偶尔出现
          Ctrl+c 退出后,再运行报端口占用
请教大神问题何在?感谢万分。11

代码如下:

  1. //多进程的方式
  2. //服务器端 server.c
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <arpa/inet.h>
  9. #include <netinet/in.h>
  10. #include <stdlib.h>

  11. #define SERVER_PORT     1234              //1024以前是系统内部占用
  12. #define SERVER_IPADDR   "192.168.31.76"   //注意:IP地址需要和自己Ubuntu系统里面的IP地址,设置为同网段的。
  13. #define LISTEN_Q_MAX    10                //服务器能够连接的客户端总个数。
  14. //typedef 类型别名  int a char b;
  15. typedef struct sockaddr_in SIN;
  16. int Socket(int domain,int type,int protocol);
  17. int Bind(int sockfd,SA* my_addr,int addrlen);
  18. int Listen(int s,int backlog);
  19. int Accept(int s,SA* addr,int *addrlen);
  20. //声明全局变量
  21. char t_buf[64];
  22. char r_buf[64];
  23. char clientname[64];
  24. int r_len;

  25. int main(int argc,char* argv[])
  26. {
  27. pid_t pid;
  28. int serfd,clifd;
  29. int addrlen;
  30. int reuse=1;
  31. SIN seraddr,cliaddr;

  32. //1、创建一个用于TCP通信的端口
  33. serfd = Socket(AF_INET,SOCK_STREAM,0);

  34. //2、编写服务器的地址信息
  35. bzero(&seraddr,sizeof(SIN));  //功能之前用的memset()
  36. seraddr.sin_family = AF_INET;
  37. seraddr.sin_port = htons(SERVER_PORT);  //配置端口号
  38. seraddr.sin_addr.s_addr=inet_addr(SERVER_IPADDR); //对IP地址传进来

  39. setsockopt(serfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)); //不是必须的

  40. //3、端口和服务器进行绑定
  41. Bind(serfd,(SA*)&seraddr,sizeof(SA));

  42. //4、创建监听队列
  43. Listen(serfd,LISTEN_Q_MAX);

  44. //5、等待客户端连接,获取客户端信息
  45. while(1)
  46. {
  47.   clifd = Accept(serfd,(SA*)&cliaddr,&addrlen);
  48.   //等待被连接
  49.         //clifd=accept(serfd,(SA*)&cliaddr,&addrlen);
  50.         if(clifd == -1)
  51.         {
  52.          perror("accept failed\r\n");
  53.         exit(0);
  54.        }
  55.   
  56.   
  57.   
  58.   printf("Connect already.client%d\r\n",clifd);
  59.   
  60.   pid = fork();
  61.   if(pid == 0)
  62.     {
  63.       //子进程
  64.        read(clifd,clientname,sizeof(clientname));
  65.        while(1)
  66.        {
  67.       //子进程 --处理当前客户端的事务
  68.       r_len = read(clifd,r_buf,sizeof(r_buf));
  69.       if(r_len == 0)
  70.        {
  71.        close(clifd);
  72.        printf("client %s offline\r\n",clientname);
  73.        exit(0);
  74.       }
  75.       printf("recv from client %s:%sr\n",clientname,r_buf);
  76.       
  77.        }     
  78.     }
  79.    close(clifd);
  80.    close(serfd);
  81.    }

  82. int Socket(int domain,int type,int protocol)
  83. {
  84. int serfd;        //局部变量
  85. serfd = socket(domain,type,protocol);  //核心
  86. if(serfd == -1)
  87. {
  88.       perror("socket failed\r\n");
  89.    exit(-1);
  90. }

  91. return serfd;
  92. }


  93. int Bind(int sockfd,SA* my_addr,int addrlen)
  94. {
  95. int ret;
  96. ret = bind(sockfd,my_addr,addrlen);
  97. if(ret == -1)
  98. {
  99.   perror("bind failed\r\n");
  100.   exit(-1);
  101. }
  102.      return 0;
  103. }

  104. //监听函数
  105. int Listen(int s,int backlog)
  106. {
  107. int ret;
  108. ret = listen(s,backlog);
  109. if(ret == -1)
  110. {
  111.   perror("listen failed\r\n");
  112.   exit(-1);
  113. }
  114. return 0;
  115. }

  116. //等待接收客户端函数
  117. int Accept(int s,SA* addr,int *addrlen)
  118. {
  119. int ret;
  120. ret = accept(s,addr,addrlen);
  121. if(ret == -1)
  122. {
  123.   perror("accept failed\r\n");
  124.   exit(-1);
  125. }
  126. return ret;
  127. }</p>
复制代码




+10
回复

使用道具 举报

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

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-6-2 20:04 , Processed in 0.071910 second(s), 36 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

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