H DFTACTGRP(*NO) ACTGRP(*NEW) /copy socket_h /copy errno_h /copy signal_h D translate PR ExtPgm('QDCXLATE') D length 5P 0 const D data 32766A options(*varsize) D table 10A const D init_signals PR D got_alarm PR D signo 10I 0 value D msg S 50A D sock S 10I 0 D port S 5U 0 D addrlen S 10I 0 D addr s 20I 0 D ch S 1A D host s 32A D addr_port s 6A D port_status s 1A D file s 32A D p_Connto S * D RC S 10I 0 D Request S 60A D ReqLen S 10I 0 D RecBuf S 50A D RecLen S 10I 0 C************************************************* C* The user will supply a hostname and port C* name as parameters to our program... C************************************************* c *entry plist c parm host c parm addr_port c parm port_status c eval port = %INT(addr_port) c eval *inlr = *on C************************************************* C* Get the 32-bit network IP address for the host C* that was supplied by the user: C************************************************* c eval addr = inet_addr(%trim(host)) c if addr = INADDR_NONE c eval p_hostent = gethostbyname(%trim(host)) c if p_hostent = *NULL c eval msg = 'Unable to find that host!' c* dsply msg c return c endif c eval addr = h_addr c endif C************************************************* C* Create a socket C************************************************* c eval sock = socket(AF_INET: SOCK_STREAM: c IPPROTO_IP) c if sock < 0 c eval msg = 'Error calling socket()!' c*end dsply msg c return c endif C************************************************* C* Create a socket address structure that C* describes the host & port we wanted to C* connect to C************************************************* c eval addrlen = %size(sockaddr) c alloc addrlen p_connto c eval p_sockaddr = p_connto c eval sin_family = AF_INET c eval sin_addr = addr c eval sin_port = port c eval sin_zero = *ALLx'00' C************************************************* C* Connect to the requested host C************************************************* C/FREE init_signals(); alarm(5); if connect(sock: p_connto: addrlen) < 0; msg = 'unable to connect to server!'; // dsply msg; callp close(sock); alarm(0); port_status = '0'; return; else; msg = 'Host is online!'; port_status = '1'; // dsply msg; endif; /end-free C************************************************* C* We're done, so close the socket. C* do a dsply with input to pause the display C* and then end the program C************************************************* c callp close(sock) c* dsply pause 1 c return C************************************************* C* We're going to use signals to indicate timeouts C* so we have to enable signals in the program C************************************************* P init_signals B D init_signals PI D act ds likeds(sigaction_t) /free Qp0sEnableSignals(); sigemptyset(act.sa_mask); sigaddset(act.sa_mask: SIGALRM); act.sa_handler = %paddr(got_alarm); act.sa_flags = 0; act.sa_sigaction = *NULL; sigaction(SIGALRM: act: *omit); /end-free P E C************************************************* C* We need a stub to handle the alarm C************************************************* P got_alarm B D got_alarm PI D signo 10I 0 value /free // Do nothing. The connect() API will return // EINTR ("interrupted by signal") when the // signal is received. /end-free P E P