2013-08-27 80 views
0

我已經抓住Lidgren的最新版本,從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(); 
     } 
    } 
} 

的服務器獲取狀態改變每次連接時間(?從運行到運行狀態的變化?) 然後我得到它需要客戶端的時間調試消息/服務器

但我從來沒有獲取數據信息。此代碼是否可以在別人的機器上工作?有什麼明顯的我失蹤?

謝謝。

+0

我發現了這個問題,但我不明白爲什麼這是一個問題。通過簡單地添加「Thread.Sleep(500);」在客戶端連接之後(在發送數據消息之前)它正常工作。我不明白延遲是如何起作用的。這實際上是lidgren服務器註冊連接以批准消息所用的時間嗎? –

回答

2

的問題是,所述連接時間(在其自己的消息)和所述第一數據消息之間,連接一直沒在服務器端完全設置。作爲我的證明,我只是簡單地加了一個延遲(Thread.Sleep(500))。爲了更有效的解決方案,我計劃在客戶端發送更多消息之前實施來自服務器的響應消息。

1

如果你的意圖是,一旦你連接,當您從服務器接收一個statuschange更新通知,您已經連接的連接不會阻止,也沒有阻止隨後的代碼,如果執行,你應該把你的信息發送郵件你還沒有建立連接。

+0

這是一個改進初始握手協議的好方法。 –