#include #include #include #include #include #include #include #include #define FTNLEN short /* ftnlen defined in f2c.h */ void init_sockaddr (name, hostname, port) struct sockaddr_in *name; char *hostname; unsigned short int port; { struct hostent *hostinfo; name->sin_family = AF_INET; name->sin_port = htons (port); hostinfo = gethostbyname (hostname); if (hostinfo == NULL) { fprintf (stderr, "Unknown host %s. \n", hostname); exit (EXIT_FAILURE); } name->sin_addr = *(struct in_addr *) hostinfo->h_addr; } /* **- SUBROUTINE CONNECT_TCP(SOCK_NUM,HOST_NAME) */ void connect_tcp_ (sock_num,host_name,host_len) int *sock_num; char *host_name; FTNLEN host_len; { int sock, status; unsigned short int port; char cmd; struct sockaddr_in servername; port = (unsigned short int) *sock_num; /* Create the socket */ sock = socket (AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror ("socket (client)"); exit (EXIT_FAILURE); /* *sock_num = -1; return; */ } /* Connect to the server */ init_sockaddr (&servername, host_name, port); if (0 > connect (sock, (struct sockaddr *) &servername, sizeof (servername))) { /* perror ("connect (client)"); exit (EXIT_FAILURE); */ close (sock); *sock_num = -1; return; } *sock_num = sock; } /* **- SUBROUTINE SEND_TCP(SOCK_NUM,BUFF,LEN) */ void send_tcp_ (sock_num, buff, len) int *sock_num, *len; char *buff; { int status, clen; clen = *len; do { if (0 > (status = write (*sock_num, buff, clen))) { perror ("\nwrite(send_rt)"); exit (EXIT_FAILURE); } clen -= status; buff += status; } while (clen); } /* **- SUBROUTINE RECV_TCP(SOCK_NUM,BUFF,LEN) */ void recv_tcp_ (sock_num, buff, len) int *sock_num, *len; char *buff; { int status, clen; clen = *len; do { if (0 > (status = read (*sock_num, buff, clen))) { perror ("\nread(recv_rt)"); exit (EXIT_FAILURE); } clen -= status; buff += status; } while (clen); } /* **- SUBROUTINE DISCONNECT_TCP(SOCK_NUM) */ void disconnect_tcp_ (sock_num) int *sock_num; { close (*sock_num); }