Quick startHow to set up a Server or Client// create a configuration
NetAppConfiguration myConfig = new NetAppConfiguration("MyApp", 14242); // application identifier and local port (can be omitted for clients)
myConfig.MaximumConnections = 32; // necessary for server only
// create a log
NetLog log = new NetLog();
// create the server/client and start listening
NetServer myNet = new NetServer(myConfig, log);
// or...
NetClient myNet = new NetClient(myConfig, log);How to connect to a server (client only)myNet.Connect("localhost", 12345); // takes IP number or hostnameHow to detect connects/disconnects
myNet.StatusChanged += new EventHandler<NetStatusEventArgs>(myNet_StatusChanged);// to track connect/disconnect etc
...
void myNet_StatusChanged(object sender, NetStatusEventArgs e)
{
// e.Connection is the connection for which status changed
// e.Connectionn.Status is the new status
// e.Reason is a human readable reason for the status change
}How to read messagesbool keepGoing = true;
while (keepGoing)
{
myNet.Heartbeat();
NetMessage msg;
while ((msg = myNet.ReadMessage()) != null)
{
string str = msg.ReadString(); // <- for example
Console.WriteLine("You got a packet containing: " + str);
}
}Heartbeat() must be called regularely; it reads pending packets, sends queued packets and acknowledges. ReadMessage() will read an available message from any connection; the sender connection is found in the message property “Sender” (always same as myNet.ServerConnection on the client). ReadMessage() will return null if there is no message to read. This example assumes a string has been written (using msg.Write(String)) into the message. How to send a messageNetMessage outMsg = new NetMessage();
outMsg.Write("Hello!");
myClient.SendMessage(outMsg, NetChannel.ReliableUnordered); // <- for example
... or ...
myServer.SendMessage(outMsg, conn, NetChannel.Unreliable); // <- to send to connection 'conn'There are 4 types of channels: Unreliable - Messages may or may not arrive Sequenced - Messages may or may not arrive. If a message arrives late, ie. a newer message has already arrived, the late packet will be dropped ReliableUnordered - Messages will arrive, but not necessarily in the order they were sent. Ordered - Messages will arrive, in the same order they were sent. All types have duplicate detection (even the unreliable type) which ensures that a packet will not arrive multiple times. How to discover local servers (client only)myNet.ServerDiscovered += new EventHandler<NetServerDiscoveredEventArgs>(OnServerDiscovered);
...
myNet.DiscoverLocalServers(12345); // supply server port here
...
void OnServerDiscovered(object sender, NetServerDiscoveredEventArgs e)
{
// We found a server located at e.ServerInformation.RemoteEndpoint
}How to enable encryptionRun the GenerateEncryptionKeys application; it will display two strings to add to your configuration; a shorter line for the client and a larger line for the server; example: Client:
myConfig.EnableEncryption(
"AQABrHL6pA9v51uy61HUxFyzOUNxrBgv+CRHevLHR30EcxdGKDjkxSYrm5CLBDxKg" +
"kS7cXwViSCMGpqRPO5tpWcXO+2tpQinJ8AqEKxFwmqeY0B/RdLBPCGbdcU6jVxfEG" +
"VF3MvjQ3xetkHh4O3/FfeWHrvPM7hkODI6Veeo7E9+wAU=", null);
Server:
myConfig.EnableEncryption(
"AQABrHL6pA9v51uy61HUxFyzOUNxrBgv+CRHevLHR30EcxdGKDjkxSYrm5CLBDxKg" +
"kS7cXwViSCMGpqRPO5tpWcXO+2tpQinJ8AqEKxFwmqeY0B/RdLBPCGbdcU6jVxfEG" +
"VF3MvjQ3xetkHh4O3/FfeWHrvPM7hkODI6Veeo7E9+wAU=",
"qgKIuulFJ0+t6nS4UAYb4t5s2H7X+8i4d6uVbxL/Kf/N+KVuVDa3UxyDEN/njNn5+" +
"X2dNZ1Swn+TpABjNGeX7Kyoq6uWSNwE5Sr+Crt9Hrnw1gD4xhDtv9kNPyF5R8KuiP" +
"+ekzFkbNYPwiZ2fxmX+s/oyHtAyYDJLBGLpN1i8plCtxDR0wIVg6WFF0EDWiuUUVb" +
"Hg7gEITf0TgLTeYIInqkZ2SxaovUXQWNQ/SDRZRF/vttiEbhoYtyK6B49al93FSSm" +
"JXBQaKuQuwpFdFQ9eoCM1dcg8yqOLmEbvX9ki9yKy9iCOGu5kMSCH886LMXXqnLvJ" +
"2vqT5mRbpTZ3qG5C8vLuvgskRqvladbdnNf7Bn+ij9ty2MzzAtDNuLSA1undEc5qz" +
"yI7KwCT7RwOSEFy0q2xwsPr/foSzhpd6UQEvHU63Y7Kfk/6ZkcS9Kav8vrxAy5+1T" +
"oV2qSAei0xMpVTfl/sq9mzYUIa74oEqwk7KaQF6zvRW6XgkyXZtNgYuU7z1dFwkSj" +
"h0IOHq7swUf4SmRCl07TdhQzXbU7IS2f0q/tUksVJG1TQhS4MCoPPvALCxftFyO6u" +
"VINkOxKJBGbvw=="); How to shutdown gracefullymyNet.Shutdown("Bye everyone");All connections will be closed and the supplied string given as reason.
|
Regarding the status changed line: myNet.StatusChanged?
It doesnt appear that netpeer/netclient have this StatusChanged? event (anymore?).