We hope the following tutorial helps get you up and running as quickly as possible. If you feel something is missing or confusing please leave a comment below. If this tutorial is not exactly what you were after please see our tutorials section for more information.
What you need to get up and running:
- Visual Studio 2010 Express or better (Including .Net4).
- The latest release of NetworkComms.Net. Please see our Download section.
Then what?
Once you have everything you need you probably want to start working your way through the included examples.
Part 1 – Compile the examples …
- Extract the downloaded zip file to a suitable location.
- Browser to the location chosen in step 1 and open the examples solution file (Examples\NetworkComms.Net Examples.sln). This should open in Visual Studio. Note: You may see notifications related to Unsupported project types for platforms such as Windows Phone, iOS etc, due to missing necessary SDKs, you can safely ignore these notifications.
- Compile all projects by hitting F6 or right clicking on the solution within Visual Studio’s Solution Explorer Window and select ‘Build Solution’. This creates the necessary project executables. Note: If you see any build errors due to namespaces ‘ProtoBuf’ or ‘NLog’ please ensure your NuGet Package Manager is correctly configured. These are external packages/dependencies that should be downloaded when you first build the solution.
Part 2 – Run the basic example …
- You can run all of the examples on the same computer or across different ones. For the sake of this demonstration we will run them both locally (Just make the appropriate IP change when requested if running on different computers).
- Browse to the location where you extracted the solution in Part 1. Now browse to “ExamplesConsole\bin\Debug\” . This directory should contain the executables compiled in Part 1. If not make sure Visual Studio is configured to build Debug.
- Execute “ExamplesConsole.exe”. This should open in a new console window. Press ‘n’ to leave logging disabled.
- Run the basic example by selecting option ‘1’.
- The output of the console should be reset and now start with a line saying “Listening for messages on:” . What follows is a list of all IPs detected on the local machine and the port number now opened by the example. You may be presented with various firewall messages asking if you trust the application to open the desired port, ensure you DO give the necessary permissions.
- You now need to open a second instance of “ExamplesConsole.exe”. This can be on the same or different computer. Make sure you select the basic example again using option ‘1’.
- The output of the second instance should again start with “Listening for messages on:” .
- You can now send messages from one instance to the other using NetworkComms.Net. In the instance of your choice type a message as directed and press return.
- Enter the IP address and port of the destination instance. This should be in the form IPAddress:Port, e.g. 192.168.1.56:10000.
- Press enter and hopefully the message appears in the destination instance.
- Checkout the source code to see how short the example actually is. You can view the example source in Visual Studio by browsing to the ‘BasicSend.cs’ code file within the ExamplesConsole project. The code is also included below.
- Bear in mind it is possible to break the basic example. No error handling was included to help keep things as short as possible.
Part 3 – Run the other examples and discover the more advanced features available …
- Execute “ExamplesConsole.exe” as in part 2 and following directions provided when the other examples are selected. If you have any problems see here.
public static void RunExample() { //We need to define what happens when packets are received. //To do this we add an incoming packet handler for a 'Message' packet type. // //We will define what we want the handler to do inline by using a lambda expression //http://msdn.microsoft.com/en-us/library/bb397687.aspx. //We could also just point the AppendGlobalIncomingPacketHandler method //to a standard method (See AdvancedSend example) // //This handler will convert the incoming raw bytes into a string (this is what //the <string> bit means) and then write that string to the local console window. NetworkComms.AppendGlobalIncomingPacketHandler<string>("Message", (packetHeader, connection, incomingString) => { Console.WriteLine("\n ... Incoming message from " + connection.ToString() + " saying '" + incomingString + "'."); }); //Start listening for incoming 'TCP' connections. //We want to select a random port on all available adaptors so provide //an IPEndPoint using IPAddress.Any and port 0. //See also Connection.StartListening(ConnectionType.UDP, IPEndPoint) Connection.StartListening(ConnectionType.TCP, new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)); //Print the IP addresses and ports we are listening on to make sure everything //worked as expected. Console.WriteLine("Listening for TCP messages on:"); foreach (System.Net.IPEndPoint localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.TCP)) Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port); //We loop here to allow any number of test messages to be sent and received while (true) { //Request a message to send somewhere Console.WriteLine("\nPlease enter your message and press enter (Type 'exit' to quit):"); string stringToSend = Console.ReadLine(); //If the user has typed exit then we leave our loop and end the example if (stringToSend == "exit") break; else { //Once we have a message we need to know where to send it //We have created a small wrapper class to help keep things clean here ConnectionInfo targetServerConnectionInfo = ExampleHelper.GetServerDetails(); //There are loads of ways of sending data (see AdvancedSend example for more) //but the most simple, which we use here, just uses an IP address (string) and port (integer) //We pull these values out of the ConnectionInfo object we got above and voila! NetworkComms.SendObject("Message", ((IPEndPoint)targetServerConnectionInfo.RemoteEndPoint).Address.ToString(), ((IPEndPoint)targetServerConnectionInfo.RemoteEndPoint).Port, stringToSend); } } //We should always call shutdown on NetworkComms.Net if we have used it NetworkComms.Shutdown(); }
Projects in Examples Solution:
The following projects have been included to help demonstrate the majority of NetworkComms.Net features:
- ExamplesChat.Android – This is the ported version of the WPF chat application to Android devices. Regardless of platform all chat applications can communicate.
- ExamplesChat.iOS – This is the ported version of the WPF chat application to iOS devices.
- ExamplesChat.WinRT – This is the ported version of the WPF chat application to WinRT/Windows Store.
- ExamplesChat.WP8 – This is the ported version of the WPF chat application to Windows Phone 8.
- ExamplesChat.WPF – A simple WPF client server chat example that demonstrates the core features, i.e. TCP, UDP, Encryption etc.
- ExamplesConsole – A large number of console based examples written in C#, that attempt to demonstrate the majority of library features.
- ExamplesConsole.VB – A number of console based examples written in VB.Net. At the present time only the BasicSend example is available.
Things to also be aware of:
The download package includes suitable XML and PDB files associated with the build. We highly recommend you include these files any any of your projects, alongside the DLLs, so that you get suitable intellisense information and also easier debugging. For more information please read this article on why PDB files are useful, and this article for attaching source code.
What to do if you have problems:
You first source of help should be the forums, it may well be someone else has experienced and solved a similar problem before. If you have found a genuine error please fill out a bug report here. We also offer commercial support packages.