我正在学习使用网络库 Lidgren。我试图让一个基本的客户端/设置服务器运行,但我收到了一个错误。到目前为止,我只有服务器,当我运行它时,服务可能会运行 10 秒,然后崩溃,但Lidgren.Network.NetException
. 如果我删除第一个循环,则不会发生这种情况while
,该循环旨在继续检查消息。有关异常的附加信息是:Additional information: Trying to read past the buffer size - likely caused by mismatching Write/Reads, different size or order.
谁能帮我这个?另外,我是否可以通过尝试异步读取消息来避免这种情况,如果是这样,我将如何做到这一点?
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();
}
}
}