|
C# Asynchronous Client Program Example
Create a new console application project and you might want to use the solution name, AsyncTcpClientCS and project name, AsyncTcpClient as shown in the following Figure.
|
Rename the source file to AsyncClient to reflect the application that we are going to develop.
Add the following using directive. Take note that the AsyncPacketClass also included, and then we need to add a reference to the class later.
using System; using System.Net; using System.Net.Sockets; using System.Collections; using System.Threading; using AsyncPacketClass; |
Add a reference to the AsyncPacketClass class library. Select project folder > Right click mouse > Select Add Reference context menu.
Browse and find the AsyncPacketClass DLL file. It should be under the project’s Debug (Release) folder.
Select the DLL file and click OK.
You should see the reference under the References folder in the Solution Explorer as shown below.
You can see the details of the class by double clicking the class node. All available objects in the class can be seen in the Object Browser.
Next, add the usage() method.
/// <summary> /// Displays simple usage information /// </summary> static public void usage() { Console.WriteLine("Executable_file_name [-c count] [-n address] [-p port]"); Console.WriteLine("Available options:"); Console.WriteLine(" -c count Number of simultaneous asynch connects (BeginAccept) to create"); Console.WriteLine(" -n address Server address to connect to"); Console.WriteLine(" -p port Port number to connect to server on"); Console.WriteLine(); } |
Then, add th Main() code.
/// <summary> /// This is the main program that parses the command line parameters and sets up each TCP /// listening socket by creating a TcpServer object to handle client connections to each /// listening endpoint. The TcpServer object creates a ClientConnection object for each /// established connection which handles IO operations from each client. /// </summary> /// <param name="args"></param> static void Main(string[ ] args) { ArrayList clientList = new ArrayList(); ManualResetEvent clientListEmpty = new ManualResetEvent( false ); string serverName = "localhost"; int connectCount = 10, serverPort = 5150;
if (args.Length != 0) { // Parse the command line for (int i = 0; i < args.Length; i++) { try { if ((args[i][0] == '-') || (args[i][0] == '/')) { switch (Char.ToLower(args[i][1])) { case 'c': // How many client connections to establish to server connectCount = System.Convert.ToInt32(args[++i]); break; case 'n': // Server address/name to connect to serverName = args[++i]; break; case 'p': // Port on which server is listening on serverPort = System.Convert.ToInt32(args[++i]); break; default: usage(); return; } } } catch { usage(); return; } } } else { usage(); return; }
try { // Resolve the host name and create 'connectCount' number of // ClientConnection objects for each address returned from name resolution. Console.WriteLine("Resolving the hostname & creating connection count..."); IPHostEntry resolvedHost = Dns.GetHostEntry( serverName ); foreach (IPAddress addr in resolvedHost.AddressList) { IPEndPoint serverEndPoint = new IPEndPoint( addr, serverPort ); ClientConnection clientConn;
for(int i=0; i < connectCount ;i++) { clientConn = new ClientConnection( serverEndPoint, clientList, clientListEmpty );
Monitor.Enter( clientList ); clientList.Add( clientConn ); Monitor.Exit( clientList ); } } } catch ( Exception err ) { Console.WriteLine("Error occurred: {0}", err.Message); }
// Wait until all the TCP server sockets are done Console.WriteLine("Waiting until all the TCP server sockets are done..."); while ( true ) { clientListEmpty.WaitOne( ); clientListEmpty.Reset(); Console.WriteLine("Connected clients: {0}", clientList.Count );
if ( clientList.Count == 0 ) break; } } |
Build and run the project.
Under the project’s Debug folder, you can see the executable and the DLL file that we refer in the project. In order to run the executable, we need to have the DLL together.
The following is the sample output.
Let test this client program with the server program. We will use the previously created server program. In this case, you may want to test several client programs against the server.
Firstly, run the server program and then run several client programs (from different paths in the same host or from different hosts). You may want to test this asynchronous client-server program in the real network either private or public.
The following snapshots show the server and the client program when run from the command prompt.
Firstly, run the server program.
Then, run several client programs against the server program.
The following snapshot shows the server output with two completed connections from clients.