简体   繁体   English

Lidgren没有发送/接收数据消息

[英]Lidgren is not sending/receiving Data messages

I have grabbed the latest edition of Lidgren, from https://code.google.com/p/lidgren-network-gen3/ 我已经从https://code.google.com/p/lidgren-network-gen3/获取了Lidgren的最新版本。

I've reviewed many tutorials, but none seem to work. 我查看了许多教程,但似乎都没有用。 I presume I must be missing something in my code. 我想我必须在代码中缺少某些内容。

using Lidgren.Network;
using System;
namespace LGServer
{
    class Program
    {
        static void Main(string[] args)
        {
            NetPeerConfiguration config = new NetPeerConfiguration("test");
            config.Port = 5432;
            config.LocalAddress = new System.Net.IPAddress(new byte[] { 127, 0, 0, 1 });
            config.MaximumConnections = 1000;
            NetServer server = new NetServer(config);
            server.Start();
            NetIncomingMessage msg = null;
            while (true)
            {
                while ((msg = server.ReadMessage()) != null)
                {

                    Console.WriteLine(msg.MessageType.ToString());
                    if (msg.MessageType == NetIncomingMessageType.Data)
                    {
                        Console.WriteLine(msg.ReadInt16());
                        Console.WriteLine(msg.ReadString());
                    }
                }
            }
        }
    }
}
//// My client code:
using Lidgren.Network;
using System;
namespace LGClient
{
    class Program
    {
        static void Main(string[] args)
        {
            NetPeerConfiguration config = new NetPeerConfiguration("test");
            NetClient client = new NetClient(config);
            client.Start();
            client.Connect("127.0.0.1", 5432);
            NetOutgoingMessage msg = client.CreateMessage();
            msg.Write((Int16)3346);
            msg.Write("Test Message 1 whooahaa");
            client.SendMessage(msg, NetDeliveryMethod.ReliableUnordered);
            client.FlushSendQueue();
        }
    }
}

The server gets a status change every time I connect (?status changes from running to running?) Then I get a debug message with the time it takes between client/server 每次我连接时,服务器都会发生状态更改(状态从运行状态变为正在运行状态?)然后我会收到一条调试消息,其中包含客户端/服务器之间的时间

But I never get the data message. 但是我从来没有收到数据信息。 does this code work on someone elses machine? 这段代码在别人的机器上工作吗? is there something obvious I'm missing? 有什么明显的我想念的吗?

Thanks. 谢谢。

The problem is that between the time connection (a message on its own) and the first data message, the connection had not been fully setup on the server side. 问题是在时间连接(一条消息本身)和第一条数据消息之间,该连接尚未在服务器端完全建立。 As a hack on my proof, I simply added a short delay (Thread.Sleep(500)). 作为证明,我只是添加了一个短暂的延迟(Thread.Sleep(500))。 For a more effective fix, I plan on implementing a response message from the server prior to the client sending more. 为了获得更有效的解决方案,我计划在客户端发送更多消息之前从服务器实施响应消息。

如果您打算在连接后立即发送消息,则应在从服务器收到状态更改更新时发送消息,以通知您已连接,因为Connect不会阻止也不阻止后续代码的执行(如果您尚未连接)建立连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM