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.
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 on it to connect to the server. We use
select() to monitor for socket input.
select() also monitors for terminal/keyboard...