To); Break; Case Write_Status: ST = SELECT (S 1, 0, & FS, 0, & To); Break; Case EXCPT_STATUS: ST = SELECT (S 1, 0, 0, & FS, & to); Break;} return (st);} int tcp_connect (const char * host, const unsigned short port) {unsigned long non_blocking = 1; unsigned long blocking = 0; int ret = 0; char * transport = "tcp"; struct hostent * phe; / * pointer to host information entry * / struct protoent * ppe; / * pointer to protocol information entry * / struct sockaddr_in sin; / * an Internet endpoint address * / sOCKET s; / * socket descriptor and socket type * / int error; #ifdef WIN32 {WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD (2, 0); err = WSAStartup (wVersionRequested, & wsaData); if (! err = 0) {/ * Tell the user that we could not find a usable * / / * Winsock DLL. * / Printf ("can't Initial Ize socket library / n "); exit (0);}} #ENDIF MEMSET (& SIN, 0, SIZEOF (SIN)); sin.sin_family = AF_INET; IF ((Sin.sin_Port = Htons (port)) == 0 ) Err_quit ("Invalid Port /"% D / "/ n", port); / * map host name to ip address, allowing for dotted decimal * / if (Phe = gethostbyname (host)) Memcpy (& Sin.sin_addr, Phe -> h_addr, phe-> h_length; else f ((sin.sin_addr.s_addr = inet_addr (host)) == INADDR_NONE) Err_quit ("can't get /"% s / "host entry / n", Host) ;
/ * Map Transport Protocol Name to Protocol Number * / IF ((PPE = getProtobyname (Transport)) == 0) Err_quit ("can't get /"% s / "protocol entry / n", transport; / * allocate A socket * / s = socket (PF_INET, SOCK_STREAM, PPE-> P_PROTO); if (S <0) Err_quit ("Can't Create Socket:% S / N", STRERROR (Errno)); / * Connect The Socket with timeout * / # ifdef WIN32 ioctlsocket (s, FIONBIO, & non_blocking); # else ioctl (s, FIONBIO, & non_blocking); # endif // fcntl (s, F_SETFL, O_NONBLOCK); if (connect (s, (struct sockaddr * ) & sin, sizeof (sin)) == -1) {struct timevalval TV; fd_set writefds; // Set connection timeout time tv.tv_sec = 10; // seconds tv.tv_usec = 0; // millisecond fd_zero (& writefds) Fd_set (s, & writefds); if (SELECT (S 1, NULL, & WRITEFDS, NULL, & TV)! = 0) {IF (fd_isset (s, & writefds)) {INT LEN = Sizeof (Error); // One must be, mainly for firewall IF (GetSockopt (s, Sol_Socket, SO_ERROR, (Char *) & Error, & Len) <0 ) Goto error_ret; if (error! = 0) goto error_ret;} else goto error_ret; // timeout or error happen} else goto error_ret;; #ifdef Win32 ioctlsocket (s, fionbio, & blocking); # else ioctl (s, fionbio & blocking); # Endif} else {Error_ret: Close (s); err_quit ("Can't Connect TO% S:% D / N", Host, Port);} Return S;} void Err_Doit (int errnoflag, const Char * fmt, va_list ap) {int errno_save; char buf [MAX_STRING_LEN]; errno_save = errno;
VSPrintf (BUF, FMT, AP); if (errnoflag) Sprintf (Buf Strlen (BUF), ":% S", STRERROR (Errno_Save)); strcat (buf, "/ n"); fflush (stdout); FPUTS (BUF, STDERR); FFlush (NULL); Return;} / * print a message and terminate. * / void err_quit (const char * fmt, ...) {va_list ap; va_start (ap, fmt); err_doot (0 , FMT, AP); VA_END (AP); EXIT (1);} # ifdef Win32CHAR * OPTARG; Char Getopt (INT C, Char * V [], Char * OPTS) {static int now = 1; char * p; IF (now> = c) Return EOF; if (v [now] [0] == '-' && (P = Strchr (OPTS, V [NOW]))) {OPTARG = V [now 1 ]; Now = 2; return * p;} Return EOF;} # elseextern char * OPTARG; # Endif # define Required (a) if (! A) {return -1;} int init (int Argc, char * argv []) {Char C; // int = '/ 0'; i_port [0] = '/ 0'; I_PORT [0] = '/ 0'; while ((c = getopt (argc, argv, "h: p :?"))! = EOF) {if (c == '?') return -1; switch (c) {case 'h': Required (OPTARG); STRCPY (i_host, optarg); Break; Case 'P': Required (OPTARG); STRCPY (I_Port, Optarg); Break; Default: Return-1;}} / * * there is no default value for hostname, Port Number, * Password or uri * / if (i_host [0] == '/ 0' || i_port [0] == '/ 0') Return -1; Return 1;} void print_usage () {char * usage [] = {"USAGE:", "-h host name", "-p port", "eXample:", "