16BPP.net
 
 
 
 
 Previous Page   TCP Chat - Messenger   Next Page 

TCP Chat - Messenger

The Messenger is a lot smaller of a program that the server.  It connects, checks if its supplied name is okay, then lets the user spit messages until they want to quit.

 

 

There are many different constructors for TcpClient.   All of them, except for the one with no parameters, will try to connect to the supplied address & port as soon as its instantiated.  We use the one with no parameters for ours.

Our Connect() function is what will try to initialize the connection.  If we connected successfully, it will send send the message name:MY_NAME, where MY_NAME is what is provided by the user at run time.  If we're still connected after that, then we're good to start sending messages.  Note that if the user supplies a name that is the empty string, the server will boot them.

SendMessages() is our main loop for this program.  If we are connected (checked with the Running property), poll the user for some input.

  • If the input is either quit or exit, start the process to disconnect from the chat server
  • If the input is non-empty, then send the message to the server

After that, take a 10 millisecond nap (like what the server does).  Once rested, verify that we're still connected by calling _isDisconnected() on our TcpClient.

_cleanupNetworkResources() is called just to make sure our NetworkStream is closed as well as the TcpClient we're using.

 Previous Page   TCP Chat - Messenger   Next Page