I am learning to use the networking library Lidgren. I have tried to get a basic client/setup server going but I am getting an error thrown at me. So far I just have the server, when I run it the serve runs for maybe 10 seconds then crashes with an exception of Lidgren.Network.NetException
. This does not happen if I remove the first while
loop, which is intended to keep checking for messages. The additional information on the exception is this: Additional information: Trying to read past the buffer size - likely caused by mismatching Write/Reads, different size or order.
Can anyone help me with this? Also, is it likely I can avoid this by trying to read the messages asynchronously and how would I do this if so?
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lidgren.Network;
namespace ConsoleApplication3
{
class Program
{
private static void Main(string[] args)
{
NetPeerConfiguration config = new NetPeerConfiguration("MyExampleName");
config.Port = 14242;
NetServer server = new NetServer(config);
server.Start();
Console.WriteLine("Server Started...");
bool listen = true;
while (listen)
{
NetIncomingMessage msg;
while ((msg = server.ReadMessage()) != null)
{
Console.WriteLine("Started Listening...");
switch (msg.MessageType)
{
case NetIncomingMessageType.VerboseDebugMessage:
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.Data:
if (msg.ReadString() == "bye")
{
listen = false;
}
else
{
Console.WriteLine(msg.ReadString());
}
break;
case NetIncomingMessageType.ErrorMessage:
Console.WriteLine(msg.ReadString());
break;
default:
Console.WriteLine("Unhandled type: " + msg.MessageType);
break;
}
server.Recycle(msg);
}
}
Console.WriteLine("Finished Listening!");
Console.ReadLine();
}
}
}