#include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int ret; int s; unsigned short port; struct sockaddr_in sin; struct hostent *he; int val = 64 * 1024; int time_limit = 30 * 1000; int farg; struct pollfd polldata; int pollret; int so_error; socklen_t so_error_size; if (argc < 3) printf ("usage: %s \n", argv[0]); memset ((char *)&sin, 0, sizeof (sin)); he = gethostbyname (argv[1]); if (he == NULL) return(errno); port = htons (atoi (argv[2])); s = socket (AF_INET, SOCK_STREAM, 0); if (s == -1) return(errno); setsockopt (s, SOL_SOCKET, SO_RCVBUF, (char *) &val, sizeof (val)); setsockopt (s, SOL_SOCKET, SO_SNDBUF, (char *) &val, sizeof (val)); val = 1; setsockopt (s, IPPROTO_TCP, TCP_NODELAY, (char *) &val, sizeof (val)); farg = fcntl (s, F_GETFL); farg |= O_NONBLOCK; fcntl (s, F_SETFL, farg); farg = fcntl (s, F_GETFL); if (farg & O_NONBLOCK) printf ("I/O is asynchronous\n"); else printf ("I/O is synchronous\n"); sin.sin_port = (unsigned int) port; sin.sin_family = AF_INET; strncpy((char *)&sin.sin_addr, he->h_addr, he->h_length); ret = connect (s, (struct sockaddr *)&sin, sizeof (sin)); if (ret == -1) { printf ("connect() failed. errno = %d (%s)\n", errno, strerror (errno)); if (errno != EINPROGRESS) return(errno); } polldata.fd = s; polldata.events = POLLOUT|POLLERR; polldata.revents = 0; pollret = poll (&polldata, 1, time_limit); printf ("pollret = %d\n", pollret); switch (pollret) { case 1: so_error = EBADF; so_error_size = sizeof (so_error); getsockopt (s, SOL_SOCKET, SO_ERROR, &so_error, &so_error_size); if ((polldata.revents & POLLOUT) == POLLOUT && so_error == 0) printf ("No error on socket\n"); else printf ("Error on socket: so_error = %d (%s)\n", so_error, strerror (so_error)); ret = so_error; break; case 0: printf ("poll() timed out\n"); ret = errno; break; case -1: printf ("poll() failed. errno = %d (%s)\n", errno, strerror (errno)); ret = errno; break; } close (s); return(ret); }