How To Create A Client Server Application In Minutes

Are you trying to develop a client server application but are coming up against all sorts of problems? We certainly did and thats why we developed NetworkComms.Net. You can follow this quick tutorial to create a client server application in just several minutes.

If you are looking for something a little more advanced please also seehow to create a WPF client server chat application. Please also checkout ourfeaturessection to find out just how powerful our network library is.

Before we get started ensure you have one of the NetworkComms.Net packages (if not please see ourdownloadpage) and Visual Studio 2010 Express or later installed, which should come with .Net4 or later.

Create a new visual studio solution named

containing a Visual C

Once created add a second Visual C 

You should now have something that looks like this:

Visual studio solution containing the empty projects.

2. Add NetworkComms.Net DLL To Projects

The NetworkComms.Net download contains DLLs for all supported platforms but we are only interested in the Net40 Release Complete DLL. Copy this DLL to the same location as the solution we created in step 1.

We now need to add project references to the NetworkComms.Net DLL we just downloaded. Right click on the

. Within the window that opens select the Browse tab and select the DLL we just downloaded. Repeat this step for the

folder within both projects you should see the NetworkComms.Net references you just added like this:

Visual studio solution containing the projects which now have references to the NetworkCommsDotNet Core DLL.

Copy and paste the following code into the

project replacing ALL EXISTING code in the file

. Note the highlighted line which is the only line required to actually send an object to the server.

using System; using llections.Generic; using System.Linq; using System.Text; using NetworkCommsDotNet; namespace ClientApplication class Program static void Main(string[] args) //Request server IP and port number Console.WriteLine(Please enter the server IP and port in the format 192.168.0.1:10000 and press return:); string serverInfo = Console.ReadLine(); //Parse the necessary information out of the provided string string serverIP = serverInfo.Split(:).First(); int serverPort = st()); //Keep a loopcounter int loopCounter = 1; while (true) //Write some information to the console window string messageToSend = This is message + loopCounter; Console.WriteLine(Sending message to server saying + messageToSend + ); //Send the message in a single line NetworkComms.SendObject(Message, serverIP, serverPort, messageToSend); //Check if user wants to go around the loop Console.WriteLine(\nPress q to quit or any other key to send another message.); if (Console.ReadKey(true).Key == ConsoleKey.Q) break; else loopCounter++; //We have used comms so we make sure to call shutdown NetworkComms.Shutdown();

//Request server IP and port number

Please enter the server IP and port in the format 192.168.0.1:10000 and press return:

//Parse the necessary information out of the provided string

//Write some information to the console window

//Send the message in a single line

//Check if user wants to go around the loop

\nPress q to quit or any other key to send another message.

//We have used comms so we make sure to call shutdown

Copy and paste the following code into the

project replacing ALL EXISTING code in the file

. Note the highlighted lines which is all that is required to setup the server.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using NetworkCommsDotNet; using NetworkCommsDotNet.Connections; namespace ServerApplication class Program static void Main(string[] args) //Trigger the method PrintIncomingMessage when a packet of type Message is received //We expect the incoming object to be a string which we state explicitly by using string NetworkComms.AppendGlobalIncomingPacketHandlerstring(Message, PrintIncomingMessage); //Start listening for incoming connections Connection.StartListening(ConnectionType.TCP, new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)); //Print out the IPs and ports we are now listening on Console.WriteLine(Server listening for TCP connection on:); foreach (System.Net.IPEndPoint localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.TCP)) Console.WriteLine(0:1, localEndPoint.Address, localEndPoint.Port); //Let the user close the server Console.WriteLine(\nPress any key to close server.); Console.ReadKey(true); //We have used NetworkComms so we should ensure that we correctly call shutdown NetworkComms.Shutdown(); /// summary /// Writes the provided message to the console window /// /summary /// param name=headerThe packet header associated with the incoming message/param /// param name=connectionThe connection used by the incoming message/param /// param name=messageThe message to be printed to the console/param private static void PrintIncomingMessage(PacketHeader header, Connection connection, string message) Console.WriteLine(\nA message was received from + connection.ToString() + which said + message + .);

//Trigger the method PrintIncomingMessage when a packet of type Message is received

//We expect the incoming object to be a string which we state explicitly by using string

//Start listening for incoming connections

//Print out the IPs and ports we are now listening on

Server listening for TCP connection on:

//We have used NetworkComms so we should ensure that we correctly call shutdown

/// Writes the provided message to the console window

/// param name=headerThe packet header associated with the incoming message/param

/// param name=connectionThe connection used by the incoming message/param

/// param name=messageThe message to be printed to the console/param

5. Run Your Client Server Application

Ensure Visual Studio is set to build in

mode and then build all projects by pressing F6. An alternative is to right click the Solution file in visual studio and select

Browse to the compiled directory for both the client and server application. The quickest way to do this is to right click on each project and select

. Within the folder that opens look for a directory named

and then inside that

. You may see a Firewall notification at this point so it is important to give the application the appropriate permissions. You should  see something like this, although expect the IP addresses to be different:

The expected output of ServerApplication.exe

. The first piece of information you are required to enter is a valid IP address and port for the server. Look at the output of the

and select an appropriate IP address and port. If testing locally the best address will most likely start 127.0.x.x, 192.168.x.x or 10.x.x.x. Remember to follow the IP address with a colon, :, and then the port number. Once you press enter you should see the server has received its first message. From this point on just follow the instructions presented in the client.

You have successfully created a client server application using NetworkComms.Net. Stop  your timers now.

Ensure you have correctly configured your firewall to allow the necessary traffic.

If you are still have issues please post on ourforumsand we will be more than happy to help.

Exception Handling In a completed application it would be necessary to add exception handling to the client and server. There are error conditions where there is no appropriate default action, such as a target server being unavailable. In these cases you need to catch the exception and decide on your own preferred course of action. Please see our article onexception handlingfor more information.

See ourcreating a wpf chat applicationarticle which starts to cover some of the more sophisticated features.

See our onlineAPI Referencewhich explains what all of the methods do.

Adding Network Functionality To Any .Net Application in 3 Lines of Code

How To Create A Client Server Application In Minutes

Back to the open source

Version 3.0.2 has now been released. We are also working on a feature list for 3.1, please let us know if you have any suggestions.

NetworkComms.Net is a high performance cross-platform .Net C network library, which can be used to quickly add networking functionality to all of your applications.

Back to the open source