我从https://code.google.com/p/lidgren-network-gen3/买了最新版的“丽德格伦”
我复习了很多教程,但似乎都没有用。我想我一定是在代码中遗漏了什么。
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();
}
}
}每次连接时,服务器都会得到状态更改(?状态从运行到运行?)然后,我得到一条调试消息,它在客户机/服务器之间所需的时间。
但我从来没有收到过数据信息。这个代码在某人的机器上工作吗?我有什么明显的遗漏吗?
谢谢。
发布于 2013-08-27 23:04:19
问题是,在时间连接(消息本身)和第一个数据消息之间,连接还没有在服务器端完全建立。作为对我的证据的一次攻击,我只是添加了一个短延迟(Thread.Sleep(500))。为了获得更有效的修复,我计划在客户机发送更多信息之前实现来自服务器的响应消息。
发布于 2014-10-27 00:34:56
如果您的意图是在连接后立即发送消息,则应该在从服务器接收状态更改更新时发送消息,该服务器通知您已经连接,如果尚未建立连接,则连接不会阻止或阻止后续代码的执行。
https://stackoverflow.com/questions/18456344
复制相似问题