2013-12-09 52 views
-1

在工作中,我必須路由一些使用UDP進入的數據。我已經學習了一些教程,如this blogthis tutorial,並製作了兩個控制檯應用程序,一個發送UDP消息(客戶端),另一個讀取它們(監聽器)。 當我輸入IP地址127.0.0.1(本地主機)一切順利,但沒有其他地址的作品。如果我使用我真正的本地IP地址,我沒有運氣。由於我本地沒有靜態IP,我也嘗試在有靜態IP的服務器上安裝偵聽器。爲此,我已經確保將規則添加到Windows防火牆入站規則集。仍然沒有運氣。有任何想法嗎?這裏是我的代碼:UDP到服務器不起作用

/西蒙/

客戶

class Client 
{ 
    private readonly Socket _sendingSocket; 
    private readonly IPEndPoint _receiverEndpoint; 
    private static int outboundPortNumber = int.Parse(ConfigurationManager.AppSettings["OutgoingPortNumber"]); 

    public Client() 
    { 
     var receiverAddress = IPAddress.Parse(ConfigurationManager.AppSettings["IpOfReceiver"]); 
     _receiverEndpoint = new IPEndPoint(receiverAddress, outboundPortNumber); 
     _sendingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
     _sendingSocket.EnableBroadcast = true; 
    } 

    public void Transmit() 
    { 
     bool done = false; 
     while (!done) 
     { 
      Console.WriteLine("Hit q to quit"); 
      var keypress = Console.ReadKey(); 

      if (keypress.KeyChar.ToString().ToLower() == "q") 
       done = true; 
      else 
       SendTextToServer(); 
     } 
    } 

    private void SendTextToServer() 
    { 
     Console.WriteLine("Enter text to send"); 
     var textToSend = Console.ReadLine(); 
     byte[] send_buffer = Encoding.ASCII.GetBytes(textToSend); 
     try 
     { 
      _sendingSocket.SendTo(send_buffer, _receiverEndpoint); 
     } 
     catch (Exception send_exception) 
     { 
      Console.WriteLine(" Exception {0}", send_exception.Message); 
     } 
    } 
} 

監聽

class Listener 
{ 
    private bool _started; 
    private ManualResetEvent _stop; 
    private UdpClient _client; 
    private IPEndPoint _endPoint; 
    private Thread _workingThread; 

    private static int inboundPortNumber = int.Parse(ConfigurationManager.AppSettings["IncomingPortNumber"]); 

    public void Start() 
    { 
     _started = true; 
     _stop = new ManualResetEvent(false); 
     InitializeUdpClient(); 
     InitializeWorkingThread(); 
    } 

    private void InitializeUdpClient() 
    { 
     _endPoint = new IPEndPoint(IPAddress.Any, inboundPortNumber); 
     _client = new UdpClient(_endPoint); 
    } 

    private void InitializeWorkingThread() 
    { 
     _workingThread = new Thread(WorkerFunction); 
     _workingThread.Name = "WorkingThread"; 
     _workingThread.Start(); 
    } 

    private void WorkerFunction() 
    { 
     while (_started) 
     { 
      var res = _client.BeginReceive(iar => 
      { 
       if (iar.IsCompleted) 
       { 
        byte[] receivedBytes = _client.EndReceive(iar, ref _endPoint); 
        OutputToConsole(receivedBytes); 


       } 
      }, null); 

      if (WaitHandle.WaitAny(new[] { _stop, res.AsyncWaitHandle }) == 0) 
      { 
       break; 
      } 
     } 
    } 

    private void OutputToConsole(byte[] receivedBytes) 
    { 
     string receivedPacket = Encoding.ASCII.GetString(receivedBytes); 
     Console.WriteLine("{0} received at {1}", receivedPacket, DateTime.Now.ToString("hhMMss.ffff")); 
    } 
} 

- 編輯 -

一個意見的建議我應該看看一些日誌。使用Microsoft網絡監視器來捕獲我的數據流量,我已經嘗試了以下內容: (1)在開發機器上:從我的開發機器發送到127.0.0.1(從localhost到localhost)時,沒有數據被記錄。我發現這很奇怪,因爲這是收到消息並很好地打印到控制檯的唯一場景。 (2)當我從我的開發機器發送到服務器時,在我的開發機器上有一些出站數據被捕獲,並且在服務器上也有輸入數據。我在服務器上運行了netstat -b,它根本沒有顯示任何UDP協議。由於在方法InitializeUdpClient()中,UdpClient使用IPEndpoint,所以我應該綁定到端口。那爲什麼當我運行netstat時不顯示呢?基於這個新的信息,建議爲什麼聽衆沒有收到任何數據?

/simon/

+0

您的疑難解答數據包捕獲是什麼樣的? – admdrew

+0

您的錯誤報告只包含「不起作用」或「沒有運氣」等短語。怎麼了?你有錯誤嗎?套接字是否被創建? –

回答

0

對不起,這個問題是與防火牆有關。我已經啓用了在服務器上設置nibound規則以及在我的開發計算機上設置出站規則,但公司代理無聲地阻止了流量。當爲客人切換到無線網絡時,我的流量到達服務器。

Simon