Simple TCP/IP echo client
This program demonstrates a simple TCP/IP client. It will connect to an echo server and echo a string the user types.
Programming Issues
This program illustrates the classic process for a TCP/IP client program. Summarised, it is as follows:
- Create a socket with a call to
socket()
- Create and initialize a socket address structure
with the address family (
AF_INET
here), the server's remote IP address, and the port the server is listening on. - Actively connect to the server by calling
connect()
- Communicate with the server, and (in this case) allow the server to perform the active close.
Usage
Launch a server process in the background (or find one already running) and run the program to connect to it:
[paul@localhost paul]$ ./echoserv 3357 & [paul@localhost paul]$ ./echoclnt -a 127.0.0.1 -p 3357 Enter the string to echo: Echo me, please. Echo response: Echo me, please. [paul@localhost paul]$
Use the -a
and -p
switches to
specify the remote IP address and port of the server.