Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h> /* See NOTES */
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10.  
  11. int main(int argc, char **argv)
  12. {
  13. if (argc < 3) {
  14. printf("please input ip and port, for example ./main 120.12.34.56 80.\n");
  15. return -1;
  16. }
  17.  
  18.  
  19. char *ipaddr = argv[1];
  20. unsigned int port = atoi(argv[2]);
  21.  
  22. int fd = 0;
  23. struct sockaddr_in addr;
  24. fd_set fdr, fdw;
  25. struct timeval timeout;
  26. int err = 0;
  27. int errlen = sizeof(err);
  28.  
  29. fd = socket(AF_INET,SOCK_STREAM,0);
  30. if (fd < 0) {
  31. fprintf(stderr, "create socket failed,error:%s.\n", strerror(errno));
  32. return -1;
  33. }
  34.  
  35. bzero(&addr, sizeof(addr));
  36. addr.sin_family = AF_INET;
  37. addr.sin_port = htons(port);
  38. inet_pton(AF_INET, ipaddr, &addr.sin_addr);
  39.  
  40. /*设置套接字为非阻塞*/
  41. int flags = fcntl(fd, F_GETFL, 0);
  42. if (flags < 0) {
  43. fprintf(stderr, "Get flags error:%s\n", strerror(errno));
  44. close(fd);
  45. return -1;
  46. }
  47. flags |= O_NONBLOCK;
  48. if (fcntl(fd, F_SETFL, flags) < 0) {
  49. fprintf(stderr, "Set flags error:%s\n", strerror(errno));
  50. close(fd);
  51. return -1;
  52. }
  53.  
  54. /*阻塞情况下linux系统默认超时时间为75s*/
  55. int rc = connect(fd, (struct sockaddr*)&addr, sizeof(addr));
  56. if (rc != 0) {
  57. if (errno == EINPROGRESS) {
  58. printf("Doing connection.\n");
  59. /*正在处理连接*/
  60. FD_ZERO(&fdr);
  61. FD_ZERO(&fdw);
  62. FD_SET(fd, &fdr);
  63. FD_SET(fd, &fdw);
  64. timeout.tv_sec = 10;
  65. timeout.tv_usec = 0;
  66. rc = select(fd + 1, &fdr, &fdw, NULL, &timeout);
  67. printf("rc is: %d\n", rc);
  68. /*select调用失败*/
  69. if (rc < 0) {
  70. fprintf(stderr, "connect error:%s\n", strerror(errno));
  71. close(fd);
  72. return -1;
  73. }
  74.  
  75. /*连接超时*/
  76. if (rc == 0) {
  77. fprintf(stderr, "Connect timeout.\n");
  78. close(fd);
  79. return -1;
  80. }
  81. /*[1] 当连接成功建立时,描述符变成可写,rc=1*/
  82. if (rc == 1 && FD_ISSET(fd, &fdw)) {
  83. printf("Connect success\n");
  84. close(fd);
  85. return 0;
  86. }
  87. /*[2] 当连接建立遇到错误时,描述符变为即可读,也可写,rc=2 遇到这种情况,可调用getsockopt函数*/
  88. if (rc == 2) {
  89. if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
  90. fprintf(stderr, "getsockopt(SO_ERROR): %s", strerror(errno));
  91. close(fd);
  92. return -1;
  93.  
  94. }
  95.  
  96. if (err) {
  97. errno = err;
  98. fprintf(stderr, "connect error:%s\n", strerror(errno));
  99. close(fd);
  100. return -1;
  101.  
  102. }
  103. }
  104.  
  105. }
  106. fprintf(stderr, "connect failed, error:%s.\n", strerror(errno));
  107. return -1;
  108. }
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement