It will be useful for us to have a TCP client that can connect to any TCP server. This TCP client will take in a hostname (or IP address) and port number from the command line. It will attempt a connection to the TCP server at that address. If successful, it will relay data that's received from that server to the terminal and data inputted into the terminal to the server. It will continue until either it is terminated (with Ctrl + C) or the server closes the connection.
This is useful as a learning opportunity to see how to program a TCP client, but it is also useful for testing the TCP server programs we develop throughout this book.
Our basic program flow looks like this:
Our program first uses getaddrinfo() to resolve the server address from the command-line arguments. Then, the socket is created with a call to socket(). The fresh socket has connect() called...