-
Notifications
You must be signed in to change notification settings - Fork 0
Establish Connection
First to get started we need to add Network Library as reference in our solution. Grab the latest version from Github section and add it to your solution. Once that is done you can get started.
The main part of the Network Library lies in it's core components. In the NetworkLibary.Core lies all the main functions so for a simple test you only need it for basic as well as main functions.
using NetworkLibrary.Core;
Once you have added the above line to the top of the file you can now create the basis for all network communation, the INetwork interface. Add the following line in a place of your own desire. You can put it into the Main() method or add it to your class.
INetwork ourNetworkConnection;
Now that we have the basics up it's time to create our host/client.
When initiating a connection you have to decide beforehand whether you are going to be host or client. If you are trying to create a client you initiate the INetwork like following:
int hostPort = 33050;
ourNetworkConnection = new ConnectionHost(hostPort);
The parameter necessary when initiating the host is the port of the connection. In my example I chose 33050. ConnectionHost can also take one more parameter and that is the limit of active connection it should accept. If specified the host will only accept the number of active clinets specified. An example of this would be this:
int hostPort = 33050;
int limit = 7; //Our game is only an 8 player game so we only accept 7 clients.
ourNetworkConnection = new ConnectionHost(hostPort, limit);
That is how you create a host. If however you were in the process of creating a client only a slight change is necessary.
ourNetworkConnection = new ConnectionClient();
The client doesn't need any parameters.
Now that we have initiated our INetwork interface there is yet another step necessary to start, and that is to start the listener on the host. If you wanna start the listener on the host, the following line provides an example of one way to achieve that.
(ourNetworkConnection as ConnectionHost).StartBroadcasting();
This will start the listener on the host and allow it to accept incoming clients. If you don't want to stop the main program while our host is loading you can start it asyncronised. Doing that will start the listener on our host on another thread.
(ourNetworkConnection as ConnectionHost).StartBroadcastingAsync();
The above line will return the BackgroundWorker that is working on initiating the host. BackgroundWorker can return the progress of the initiation as well as an event that is run when the process is finished. With slight change and with a little help from .Net framework 3.5, we can change our above line and be immediatly notified once the host has finished initiating the listener.
(connection as ConnectionHost).StartBroadcastingAsync().RunWorkerCompleted += (a, b) =>
{
if (b.Result is Exception)
{
//An error occured while starting the listener.
}
else
{
//Everything went well, the host is up and running.
}
};
If an error occured the Result of the BackgroundWorker will contain the exception on why it happened.
Now that we have our host up and running it's time to get our client to connect to it. The process is similar as when you create the host.
string ip = "127.0.0.1";
int port = 33050;
(ourNetworkConnection as ConnectionClient).Connect(ip, port);
That's it. You can also make the client connect on another thread like we did above with our host.
(ourNetworkConnection as ConnectionClient).ConnectAsync(ip, port);
The asynchronised method returns the BackgroundWorker just like when you do with the Host. If you want to listen when the client manages to connect we only have to listen to the RunWorkerCompleted.
(ourNetworkConnection as ConnectionClient).ConnectAsync(ip, port).RunWorkerCompleted += (a, b) =>
{
if (b.Result is Exception)
{
//An error occured while starting the listener.
}
else
{
//Everything went well, the host is up and running.
}
};
As always, the Result contains the error if any such occurred.
Here is a full code sample on how to create a host and a client.
using System;
using NetworkLibrary.Core;
public class MainProgram
{
static INetwork theConnection;
public static void Main(params string[] args)
{
Console.Write("Type 'h' to create host or 'c' to create a client\nInput: ");
if (Console.ReadLine() == "h")
{
theConnection = new ConnectionHost(33050);
try
{
(theConnection as ConnectionHost).StartBroadcasting();
Console.WriteLine("\nHost up and running. Press any key to quit.");
}
catch (Exception e)
{
Console.WriteLine("\nError while creaing host: {0}. Press any key to quit.", e.Message);
}
}
else
{
theConnection = new ConnectionClient();
try
{
(theConnection as ConnectionClient).Connect("127.0.0.1", 33050);
Console.WriteLine("\nSucessfully connected. Press any key to quit.");
}
catch (Exception e)
{
Console.WriteLine("\nError while connecting: {0}. Press any key to quit.", e.Message);
}
}
Console.ReadKey();
}
}
Well that's it, you now have an active host and a client to connect to it. Head over to the the next section of the documentation, Registering Events, for information on how to send an receive data between the client and host.