home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / protocol / tcpip / 5097 < prev    next >
Encoding:
Text File  |  1992-11-09  |  2.8 KB  |  117 lines

  1. Newsgroups: comp.protocols.tcp-ip
  2. Path: sparky!uunet!decwrl!rtech!ingres!sweeney
  3. From: sweeney@Ingres.COM (Tony Sweeney)
  4. Subject: Re: TCP connect
  5. Message-ID: <1992Nov9.144723.6561@pony.Ingres.COM>
  6. Reply-To: sweeney@Ingres.COM (Tony Sweeney)
  7. Organization: Ask Computer Systems Inc., Ingres Division, Alameda CA 94501
  8. References: <Bx66Jn.D6@ushiva.wariat.org> <1992Nov5.122845.10541@pony.Ingres.COM>
  9. Distribution: usa
  10. Date: 9 Nov 92 14:47:22 GMT
  11. Lines: 104
  12.  
  13. I wrote:
  14. >In article <Bx66Jn.D6@ushiva.wariat.org> raw@ushiva.wariat.org (Roland Wilcher) writes:
  15. >>
  16. >>Some time ago there was a question about checking for a TCP connect
  17. >>to a possibly non functioning machine without the long wait.
  18. >>An answer was posted using connect with nonblocking mode. I don't
  19. >>have that article now but would appreciate any pointers . Target
  20. >>system would be Esix SVR4 using the socket libraries and TCP/IP.
  21. >>
  22. >>-- 
  23. >>Lack of skill dictates economy of style.             raw@ushiva.ncoast.org
  24. >>- Joey Ramone                                        raw@ushiva.wariat.org
  25. >>Roland A. Wilcher                         6207 Luther Ave. Cleve Oh. 44103 
  26. >>---------------------------------------------------------------------------
  27. >
  28. >
  29. What I meant to say was - here is some code I wrote to tickle a bug
  30. in Apollo DomainOS 10.4 tcp. Should fail at the connect() on any
  31. working system.
  32.  
  33. --------------------------cut here--------------------------------
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #include <sys/types.h>
  37. #include <sys/time.h>
  38. #include <sys/socket.h>
  39. #include <netinet/in.h>
  40. #include <netinet/tcp.h>
  41. #include <fcntl.h>
  42.  
  43. main()
  44. {
  45. struct sockaddr_in s, peer;
  46.  
  47. int sock, namelen;
  48.  
  49. int maxfd=FD_SETSIZE;
  50. fd_set inbits, outbits, errors;
  51.  
  52. struct timeval tims;
  53.  
  54. /* code starts here... */
  55.  
  56. FD_ZERO(&inbits);
  57. FD_ZERO(&outbits);
  58. FD_ZERO(&errors);
  59.  
  60. bzero((char *)&s, sizeof(s));
  61.  
  62. s.sin_family = AF_INET;
  63. s.sin_addr.s_addr = inet_addr("127.0.0.1"); /* loopback */
  64. s.sin_port = htons(12345);    /* any bogus value will do */
  65.  
  66. sock = socket(s.sin_family, SOCK_STREAM, 0);
  67.  
  68. if( sock < 0 )
  69.         {
  70.                 perror( "socket" );
  71.                 exit(1);
  72.         }
  73.  
  74.         /* set for non-blocking */
  75.  
  76. if (fcntl( sock, F_SETFL, O_NDELAY ) < 0)
  77.     {
  78.                 perror( "fcntl" );
  79.                 exit(1);
  80.         } ;
  81.  
  82. if (connect(sock, (struct sockaddr *)&s, sizeof(s)) < 0)
  83.     {
  84.     if (errno != EINPROGRESS)
  85.         {
  86.         perror("connect");
  87.         exit(1);
  88.         }
  89.     }
  90.  
  91. tims.tv_sec=4;    /* select times out after 4 seconds */
  92.  
  93. if (select(maxfd, &inbits, &outbits, &errors, &tims) < 0)
  94.                 {
  95.                 perror("select");
  96.         exit(1);
  97.                 }
  98.  
  99. namelen=sizeof(peer);
  100.  
  101. if (getpeername(sock, (struct sockaddr *)&peer, &namelen) < 0)
  102.     {
  103.     printf("Correct: no peer\n");
  104.     }
  105. else
  106.     {
  107.     printf("getpeername thinks it is connected to %s:%d\n",
  108.         inet_ntoa(peer.sin_addr.s_addr), peer.sin_port);
  109.     exit(1);
  110.     }
  111.  
  112.  
  113. exit(0);
  114. }
  115.  
  116.  
  117.