我从https://code.google.com/p/lidgren-network-gen3/获取了最新版本的 Lidgren
我已经查看了许多教程,但似乎没有一个有效。我想我的代码中一定遗漏了一些东西。
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();
}
}
}
每次连接时服务器都会发生状态变化(?状态从运行变为运行?)然后我会收到一条调试消息,其中包含客户端/服务器之间所需的时间
但我从来没有收到数据消息。这段代码可以在别人的机器上运行吗?我有什么明显的遗漏吗?
谢谢。