Network library is a simple C# Network Library that takes the complication of managing a TCP/IP connections and includes many handy features.
Network comes with the follow features:
- Simple and easy to use.
- Events.
- Native serializing support.
- Object registration.
- Support bind-able objects.
- And lots more...
When using Network Library, both the client and server share the
same interface INetwork
. As such, you can keep the core code
almost the same, whether you're dealing with the server or the
client.
Creating a server is very simple. Create an instance of
ConnectionHost
and run StartBroadcasting
:
using System;
using NetworkLibrary.Core;
public class MainProgram
{
static INetwork theConnection;
public static void Main(params string[] args)
{
//Create an instance of our host.
theConnection = new ConnectionHost(33050);
//Run the listener.
(theConnection as ConnectionHost).StartBroadcasting();
Console.WriteLine("\nHost up and running. Press any key to quit.");
//Prevent the program from closing
Console.ReadKey();
}
}
Creating a client is also simple:
using System;
using NetworkLibrary.Core;
public class MainProgram
{
static INetwork theConnection;
public static void Main(params string[] args)
{
//Create an instance of our host.
theConnection = new ConnectionClient();
//Connect to server.
(theConnection as ConnectionClient).Connect("127.0.0.1", 33050);
Console.WriteLine("\nSucessfully connected. Press any key to quit.");
//Prevent the program from closing
Console.ReadKey();
}
}
Sending events is also very easy. Just register a function with specific integer code:
int code = 0; //Code for this event
ourNetworkConnection.RegisterEvent(code, eventThatShouldBeRun);
/* ... */
private void eventThatShouldBeRun(object source, NetworkEventArgs args)
{
Console.WriteLine(source as string);
}
After that, you can easily send this type of message by simply doing:
int code = 0; //Code for this event
connection.SendEvent(code, "Hello World");
For more information, check out Establish Connection and Registering events.
Full documentation on Network Library can be found on the wiki.
Network Library is licensed under the terms of the WTFPL, see the included LICENSE file.