home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.protocols.tcp-ip
- Path: sparky!uunet!decwrl!rtech!ingres!sweeney
- From: sweeney@Ingres.COM (Tony Sweeney)
- Subject: Re: TCP connect
- Message-ID: <1992Nov9.144723.6561@pony.Ingres.COM>
- Reply-To: sweeney@Ingres.COM (Tony Sweeney)
- Organization: Ask Computer Systems Inc., Ingres Division, Alameda CA 94501
- References: <Bx66Jn.D6@ushiva.wariat.org> <1992Nov5.122845.10541@pony.Ingres.COM>
- Distribution: usa
- Date: 9 Nov 92 14:47:22 GMT
- Lines: 104
-
- I wrote:
- >In article <Bx66Jn.D6@ushiva.wariat.org> raw@ushiva.wariat.org (Roland Wilcher) writes:
- >>
- >>Some time ago there was a question about checking for a TCP connect
- >>to a possibly non functioning machine without the long wait.
- >>An answer was posted using connect with nonblocking mode. I don't
- >>have that article now but would appreciate any pointers . Target
- >>system would be Esix SVR4 using the socket libraries and TCP/IP.
- >>
- >>--
- >>Lack of skill dictates economy of style. raw@ushiva.ncoast.org
- >>- Joey Ramone raw@ushiva.wariat.org
- >>Roland A. Wilcher 6207 Luther Ave. Cleve Oh. 44103
- >>---------------------------------------------------------------------------
- >
- >
- What I meant to say was - here is some code I wrote to tickle a bug
- in Apollo DomainOS 10.4 tcp. Should fail at the connect() on any
- working system.
-
- --------------------------cut here--------------------------------
- #include <stdio.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/time.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netinet/tcp.h>
- #include <fcntl.h>
-
- main()
- {
- struct sockaddr_in s, peer;
-
- int sock, namelen;
-
- int maxfd=FD_SETSIZE;
- fd_set inbits, outbits, errors;
-
- struct timeval tims;
-
- /* code starts here... */
-
- FD_ZERO(&inbits);
- FD_ZERO(&outbits);
- FD_ZERO(&errors);
-
- bzero((char *)&s, sizeof(s));
-
- s.sin_family = AF_INET;
- s.sin_addr.s_addr = inet_addr("127.0.0.1"); /* loopback */
- s.sin_port = htons(12345); /* any bogus value will do */
-
- sock = socket(s.sin_family, SOCK_STREAM, 0);
-
- if( sock < 0 )
- {
- perror( "socket" );
- exit(1);
- }
-
- /* set for non-blocking */
-
- if (fcntl( sock, F_SETFL, O_NDELAY ) < 0)
- {
- perror( "fcntl" );
- exit(1);
- } ;
-
- if (connect(sock, (struct sockaddr *)&s, sizeof(s)) < 0)
- {
- if (errno != EINPROGRESS)
- {
- perror("connect");
- exit(1);
- }
- }
-
- tims.tv_sec=4; /* select times out after 4 seconds */
-
- if (select(maxfd, &inbits, &outbits, &errors, &tims) < 0)
- {
- perror("select");
- exit(1);
- }
-
- namelen=sizeof(peer);
-
- if (getpeername(sock, (struct sockaddr *)&peer, &namelen) < 0)
- {
- printf("Correct: no peer\n");
- }
- else
- {
- printf("getpeername thinks it is connected to %s:%d\n",
- inet_ntoa(peer.sin_addr.s_addr), peer.sin_port);
- exit(1);
- }
-
-
- exit(0);
- }
-
-
-