TCP/IP Programming Manual
THEN BEGIN
CALL paramcapture(param_msg);
END;
-- Create an open socket to do IO on.
IF ((sock := socket (AF_INET, SOCK_STREAM, 0)) < 0)
THEN BEGIN
CALL PRINT_ERROR(socket_error);
CALL CRE_TERMINATOR_(CRE^Completion^fatal);
END;
-- Look up the port number of the echo service using a socket
-- routine (echo port is well known port 7)
IF (@se := getservbyname(echo_service,TCP_PROTOCOL)) = 0D
THEN BEGIN
term_msg(no_echo_serv);
CALL CRE_TERMINATOR_(CRE^Completion^warning);
END;
-- Start filling up the sockaddr_in structure for a connect.
sin.sin_port := se.s_port; -- From getservbyname
sin.sin_family := AF_INET;
-- Check to see if address was supplied in dotted decimal format.
IF (sin.sin_addr.s_addr := inet_addr(startup_msg)) = -1D
THEN BEGIN
-- It is not dotted decimal, check to see if it can be resolved
-- in a name lookup.
@hp := gethostbyname(startup_msg);
IF (@hp = 0D)
THEN BEGIN
buf ':=' "Unknown host: "
& startup_msg FOR $INT(RTL_STRLENX_(startup_msg))
& 0; -- Null Termination.
CALL term_msg(buf);
CALL CRE_TERMINATOR_(CRE^Completion^warning);
END;
sin.sin_addr.s_addr ':=' hp.h_addr_list.ptrs FOR hp.h_length;
@host_name :=@hp.h_name;
END ELSE BEGIN
@hp := gethostbyaddr (sin.sin_addr.s_addr, 4, AF_INET);
if (@hp = 0D)
THEN BEGIN
@host_name := @startup_msg;
END ELSE BEGIN
@host_name := @hp.h_name;
END;
END;
buf ':=' "Establishing Connection to: "
& host_name FOR $INT(RTL_STRLENX_(host_name))
& 0; -- Null Termination.
CALL term_msg(buf);
IF (connect(sock,sin,$LEN(sin)) < 0)
THEN BEGIN
CALL PRINT_ERROR(connect_error);
CALL CRE_TERMINATOR_(CRE^Completion^fatal);
END;
buf ':=' "Connected" & 0;
CALL term_msg(buf);
WHILE (bytes_from_term := term_read(buf:$OCCURS(buf))) > 0
DO BEGIN
IF (send(sock,buf,bytes_from_term,0)) <= 0
THEN BEGIN
CALL PRINT_ERROR(send_error);
CALL CRE_TERMINATOR_(CRE^Completion^fatal);
END;
-- Use the following loop because the socket interface may
-- require more than one call to "recv" to get all of the
-- bytes desired. This is usually due to network fragmentation.
234 Sample Programs