TCP/IP for DOS Socket Library Reference
Appendix D. C Source Code Examples
This appendix contains and explains C source code for several example application programs using the DOS socket library functions discussed in this manual. These example programs illustrate the use of socket library functions and the principles discussed in Chapter 3, "Application Programming Interface".
The comments included in the programs explain the code. The source code for these programs is included in the NetWare Communication Services software. When you install the Toolkit, the installation program puts the source code in the WET\TOOLKIT\SAMPLE directory.
Each program is provided in both DOS and Windows forms, along with batch files to compile the programs. For each of the Windows programs, a .H file, a .RC file, and a .DEF file are provided for inclusion in the compile.
Using the Example Programs
The following examples are provided:
Using the TCP Example Programs
To use the sample TCP programs, first move the TCPCLNT.EXE and TCPSERV.EXE programs to two different computers on your network. Use the following command to start the server program on one computer, substituting the appropriate drive and directory names:
C:\NET\TOOLKIT\SAMPLE\BIN> TCPSERV
On the other computer, use the following command to start the client program, substituting the appropriate drive name, directory name, and name or IP address (server_ID) of the server computer.
C:\NET\TOOLKIT\SAMPLE\BIN> TCPCLNT server_ID
To stop the server program, reboot the computer on which it is running.
The WTCPCLNT.EXE and WTCPSERV.EXE programs run under Windows and prompt for the above information.
Using the UDP Example Programs
To use the UDP sample programs, first move the UDPCLNT.EXE and UDPSERV.EXE programs to two different computers on your network. Use the following command to start the server program on one computer, substituting the appropriate drive and directory names:
C:\NET\TOOLKIT\SAMPLE\BIN> UDPSERV
On the other computer, use the following command to start the client program, substituting the appropriate drive name, directory name, and name or IP address (serverIo) of the server computer:
C:\NET\TOOLKIT\SAMPLE\BIN> UDPCLNT server_ID
To stop the server program, reboot the computer on which it is running.
The WUDPCLNT.EXE and WUDPSERV.EXE programs run under Windows and prompt for the above information.
TCP Client Programs
TCPCLNT.C is the source file for the DOS TCP client program. The program created from this source file (TCPCLNT.EXE) takes the remote hostname (server name) as input. You provide the remote hostname as a command line parameter to the DOS program. The program then resolves the name into a valid address.
The client program creates a TCP socket and then makes a TCP connection to the specified server at the TCP ECHO port (port 7). The program sends 10,000 bytes to the server and waits for all the data to be echoed before closing the socket and then terminating. The program uses the select() function to determine whether the socket is ready to send data and whether it has received data.
The WTCPCLNT program performs the same functions as TCPCLNT, but runs under Windows.
TCP Server Programs
TCPSERV.C is the source file for the TCP server program. The program derived from this source file (TCPSERV.EXE) creates a TCP socket, binds it to the TCP ECHO port (port7), and listens on the port for incoming TCP connections from a client. When the server program receives an incoming connection, it accepts the connection. After a client has established a connection, the server program accepts data sent from that client and echoes the data back to the client. The program uses the select() function to determine whether a new connection request has been received and whether data has been received from an already established connection. These steps are repeated in an infinite loop.
The WTCPSERV program performs the same functions as TCPSERV, but runs under Windows.
UDP Client Programs
UDPCLNT.C is the source file for the UDP client program. The program derived from this source file (UDPCLNT.EXE) takes the remote hostname (server name) as input. You provide the remote hostname as a command line parameter to the program. The program then resolves the name into a valid address. It creates a UDP socket and sends UDP datagrams to the specified server at the UDP echo port (port 7). The program then waits for a reply from the server.
Because UDP is a potentially unreliable protocol, the client cannot assume that a reply will arrive. The client program sets the timeout parameter of the select() function to one second, and waits for a maximum of one second before sending the next packet. It sends ten packets, then prints the number of packets sent and the number received before automatically terminating.
The WUDPCLNT program performs the same functions as UDPCLNT, but runs under Windows.
UDP Server Programs
UDPSERV.C is the source file for the UDP server program. The program derived from this file (UDPSERV.EXE) creates a UDP socket and binds it to the UDP ECHO port (port 7). The program sets up an infinite loop that receives datagrams sent by a client and echoes them back to the client.
The WUDPSERV.C program performs the same functions as UDPSERV.C, but runs under Windows.
Batch Compilation Files
The MAKEDOS.BAT file provides commands to compile all of the DOS client and server programs using any of the four memory models and either the Microsoft C compiler or the Borland C++ compiler. You can edit the file to include the commands that suit your workstation's configuration.
The MAKEWIN.BAT file compiles all of the Windows client and server programs using the Microsoft C compiler.