[英]Networking issues, possible to resolve with async method?
I am learning to use the networking library Lidgren. 我正在学习使用网络库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
. 到目前为止,我只有服务器,当我运行它时,服务器运行了Lidgren.Network.NetException
10秒钟,然后崩溃,但Lidgren.Network.NetException
异常。 This does not happen if I remove the first while
loop, which is intended to keep checking for messages. 如果删除第一个while
循环(旨在继续检查消息),则不会发生这种情况。 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.
关于该异常的Additional information: Trying to read past the buffer size - likely caused by mismatching Write/Reads, different size or order.
: 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();
}
}
}
That error usually means you are not reading the same data types in the same order as you sent them. 该错误通常表示您没有按照与发送数据相同的顺序读取相同的数据类型。 It looks like the code above reads a string - what do you send? 看起来上面的代码读取了一个字符串-您发送什么? Also, you should call server.Shutdown() when exiting your loop. 另外,退出循环时应调用server.Shutdown()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.