/*************************************************************** * * * Copyright (c) 1990 Gordian Inc. * * * * Author: Rich Lyman * * Module: talk * * Created: Wed Aug 15 12:05:10 1990 * * Abstract: * * This program demonstrates how use a TCP socket * * connect to form a connection to a specific port * * on a terminal server. * * * * The example program will simply allow a user on a * * UNIX system to connect to a specific port on an ETS * * server and dump out some data and then read some * * user input and display it on the screen. * * * * Input: * * The user is prompted for the majority of the input * * needed for this routine. However, the target server * * must be in the /etc/hosts file for the connection * * to succeed. * * * * A user may also specify the required input as part * * of the command line. The needed parameters are: * * * * argv[1] -- server name * * argv[2] -- port number * * * * Output: * * A session will be established with the specified * * server/port number combination. It will spew a * * small amount of data to the server and then read * * from the server port until the user types an * * uppercase Q or the program is killed. * * * * Disclaimer: * * The information in this software is subject to * * change without notice and should not be construed * * as a commitment by the developers of this software * * package. * * * ***************************************************************/ /* Edit History: RKL 07-feb-94 Use TCP socket connection rather than using a RTEL protocol connection. RKL 19-nov-92 Initialized the ud data structure. RKL 17-jun-92 Adjusted for potential TLI support. KCS 07-may-91 Added message field to errs() call, and err_handler. */ #include #include #include #include #include #include #define TCP 6 main(argc,argv) int argc; unsigned char* argv[]; { char inchar, outbuf[80], srvr_nm[80]; int netchan, port_num, ret_stat; if(argc > 1) strcpy(srvr_nm, argv[1]); else{ printf("Please enter server name: "); scanf("%s", srvr_nm); } if(argc > 2) port_num = atol(argv[2]); else{ printf("Port number (1-16): "); scanf("%d", &port_num); } if(port_num <= 16)port_num += 3000; printf("Connecting to server %s, port number %d\r\n", srvr_nm, port_num); ret_stat = conn_socket(srvr_nm, port_num, &netchan); if(ret_stat < 0) exit(0); /* The connection has been made and the network socket has been returned in netchan. This value can be used in all subsequent network I/O. */ strcpy(outbuf, "\r\n\nWelcome to the exciting world of TCP socket connections..."); if((ret_stat = write(netchan, outbuf, strlen(outbuf))) < 0){ printf("Socket write error, errno %d, \r\n", errno); exit(0); } /* Now read characters from the net and echo them to the display until either a capital "Q" is entered or the program is killed. */ while (1) { if ((ret_stat = read(netchan, &inchar, 1)) <= 0) { printf("Socket read failed, errno %d\r\n", errno); break; } if(inchar == 'Q') break; write(1, &inchar, 1); } /* Close the network socket. Everything is then cleaned up and the server is ready to accept another connection. */ close(netchan); } /****************************************************************************/ /** **/ /** CONN_SOCKET **/ /** **/ /** This routine will establish a TELNET connection to a specific port **/ /** on a server and return the network channel to be used for reads and **/ /** writes. **/ /** **/ /****************************************************************************/ int conn_socket(srvr_nm, port_num, netchan) char *srvr_nm; int port_num; int *netchan; { struct hostent *hdata, *gethostbyname(); struct sockaddr_in insock; int ret_stat; /* Create a local socket. */ if((*netchan = socket(AF_INET, SOCK_STREAM, TCP)) < 0){ printf("Unable to create network socket.\r\n"); return(-1); } /* Get the host IP address. */ hdata = gethostbyname(srvr_nm); if(!hdata){ printf("Unable to find host in host table\r\n"); return(-1); } /* Set up the IP address parameters and try to connect to the server. */ insock.sin_family = AF_INET; insock.sin_port = htons(port_num); rtel_copy(*hdata->h_addr_list, &insock.sin_addr, hdata->h_length); if ((ret_stat=connect(*netchan, (struct sockaddr *) &insock, sizeof(insock))) < 0) { printf("Unable to connect to network socket, errno -- %d\r\n", errno); return(-1); } /* Looks like everything worked. The network socket is returned in netchan. Return a good return status. */ return(0); } /****************************************************************************/ /** **/ /** RTEL_COPY & RTEL_ZERO **/ /** **/ /** These two routines are here simply to avoid the endless problems **/ /** associated with trying to find bzero, bcopy and memcpy on various **/ /** architectures. Hard to believe that this is a problem, but it is. **/ /** **/ /** Note that the copy routine assumes that the buffers do not overlap. **/ /** **/ /****************************************************************************/ int rtel_copy(frombuf, tobuf, count) char *frombuf; char *tobuf; int count; { while(count--) *tobuf++ = *frombuf++; return(0); } int rtel_zero(buf, count) char *buf; int count; { while(count--) *buf++ = 0; return(0); }