Monday, May 17, 2010

A FANCIER CHAT PROGRAM

Here is a fancier version of the UDP client programs that uses "non-blocking" programming. 

The earlier version that I posted is very simple.  Here's how the simple verison works:

STEP1:  WAIT for the user to input a message.  When the user finally hits the Enter key, then transmit the message to the server and go to step 2.
STEP2:  WAIT for the server to send back a message.  When a message is received from the server, print it and then go to step1.

The problem with that approach is that while the program is waiting in STEP1 it will never check for a message from the server.  If the user never hits the Enter key, the program will never go to step2 and will never check for a message from the server.   And while the program is waiting in STEP2 it will never let the user enter a new message.  If the server never sends a message the program will be stuck in step 2 and will never go to step1.

A better approach would be to not WAIT at each step.  In STEP1, just check to see if the user hit a key.  If he didn't, then go to step 2.  In step 2, check to see if a message was received from the server.  If no message was received, go to step1.  That's called a "non-blocking" approach because the program doesn't get stuck in any particular place.

Click this link to download the Visual C# project for the 'non-blocking' version of the UDP Client program.

 UdpClientConsole_NonBlocking.ZIP

No comments:

Post a Comment