Are you trying to develop a client server application but are coming up against all sorts of problems? We certainly did and that’s 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 see how to create a WPF client server chat application. Please also checkout our features section 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 our download page) and Visual Studio 2010 Express or later installed, which should come with .Net4 or later.

Start your timers now.

1. Create Visual Studio Projects

  • Create a new visual studio solution named ‘ ClientServerExample ‘ containing a Visual C# ‘ Console Application ‘ project naming it ‘ ClientApplication ‘.
  • Once created add a second Visual C# ‘ Console Application ‘ project naming it ‘ ServerApplication ‘.
  • You should now have something that looks like this:
Visual studio solution containing the empty projects.

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 ‘ ClientApplication ‘ project and select ‘ Add Reference… ‘. Within the window that opens select the Browse tab and select the DLL we just downloaded. Repeat this step for the ‘ ServerApplication ‘ project.
  • If you expand the ‘ References ‘ 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.

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

3. Copy & Paste Client Source Code

  • Copy and paste the following code into the ‘ ClientApplication ‘ project replacing ALL EXISTING code in the file ‘ Program.cs ‘. Note the highlighted line which is the only line required to actually send an object to the server.

4. Copy & Paste Server Source Code

  • Copy and paste the following code into the ‘ ServerApplication ‘ project replacing ALL EXISTING code in the file ‘ Program.cs ‘. Note the highlighted lines which is all that is required to setup the server.

5. Run Your Client Server Application

  • Ensure Visual Studio is set to build in ‘ Debug ‘ mode and then build all projects by pressing ‘F6′. An alternative is to right click the Solution file in visual studio and select ‘ Build Solution ‘.
  • 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 ‘ Open Folder In Windows Explorer ‘. Within the folder that opens look for a directory named ‘ bin ‘ and then inside that ‘ Debug ‘.
  • Run an instance of ‘ ServerApplication.exe ‘. 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 expected output of ServerApplication.exe

  • Run an instance of the ‘ ClientApplication.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 ‘ ServerApplication.exe ‘ 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.

If You Have Problems

  1. Ensure you have correctly configured your firewall to allow the necessary traffic.
  2. If you are still have issues please post on our forums and we will be more than happy to help.

Things To Be Aware Of

  • 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 on exception handling for more information.

For More Information

  1. See our Getting Started article.
  2. See our creating a wpf chat application article which starts to cover some of the more sophisticated features.
  3. See our online API Reference which explains what all of the methods do.
  4. Ask any questions on our forums .