2017-04-13 66 views
0

我想從我的服務器中的端口2020讀取一些數據包。我運行了tcpdump -r eth1 port 2020,可以發現udp數據包正在進入端口。以下是截圖:Java代碼沒有在特定端口獲得udp數據包

enter image description here

但是,當我試圖從Java代碼讀取這些數據包,我沒有得到任何數據。以下是我的java代碼:

package packetreceiver; 

import java.io.FileNotFoundException; 
import java.io.PrintWriter; 
import java.io.UnsupportedEncodingException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 
import java.net.InetSocketAddress; 
import java.net.SocketException; 
import java.net.UnknownHostException; 

import main.Main; 
import propertyfilereader.PropertyFileReader; 

import org.apache.log4j.Logger; 

import com.sun.jmx.snmp.SnmpMessage; 

import alarmprocessor.AlarmProcessor; 

public class UDPPacketReceiver extends Thread { 
    private DatagramSocket socket = null; 
    private Logger logger = Logger.getLogger(UDPPacketReceiver.class); 
    public boolean dynamicKey = false; 

    private UDPPacketReceiver() { 
     try { 

      logger.debug(" port : " + Main.orgPort); 
      logger.debug(" ip : " + Main.orgBindIPStr); 
      logger.debug("creating socket"); 

      socket = new DatagramSocket(null); 
      //socket = new DatagramSocket(38567); 
      InetSocketAddress address = new InetSocketAddress(2020); 
      socket.bind(address); 

      logger.debug("Receiver Socket created successfully"); 
     } catch (Exception e) { 
      logger.fatal("Exception at creating socket ", e); 
      logger.fatal("Exiting successfully "); 
      System.exit(0); 
     } 
     this.running = true; 
    } 

    static UDPPacketReceiver instance = null; 

    public static UDPPacketReceiver getInstance() { 
     if (instance == null) { 
      createInstance(); 
     } 
     return instance; 
    } 

    boolean running = false; 

    private static synchronized UDPPacketReceiver createInstance() { 
     if (instance == null) { 
      instance = new UDPPacketReceiver(); 
     } 
     return instance; 
    } 

    public void run() { 

     byte[] data = new byte[2048]; 
     DatagramPacket packet = new DatagramPacket(data, data.length); 

     while (this.running) { 
      try { 
       logger.debug("waiting for received data"); 
       this.socket.receive(packet); 
       int length = packet.getLength(); 
       logger.debug("length:"+length); 
       logger.debug("data:"+new String(packet.getData()));   

      } catch (Exception e) { 
       if ((this.socket == null) || (this.socket.isClosed())) { 
        this.running = false; 
       } else { 
        this.logger.fatal("Error in receiving UDP packet:", e); 
       } 
      } 
     } 
    } 

    public void shutdown() { 
     this.running = false; 
     try { 
      if ((this.socket != null) && (!this.socket.isClosed())) { 
       this.socket.close(); 
      } 
     } catch (Exception localException) { 
     } 
     this.socket = null; 
    } 
} 

我已經在stackeoverflow和其他帖子中搜索,但找不到任何解決方案。你能幫我麼。

問候, 坦維爾

+0

你有沒有試圖調查使用Wireshark的數據包? –

+0

是的。那些數據包似乎是正確 – Rumel

+0

我測試了你的代碼而沒有改變它,沒有任何東西看起來錯了。 – Hossein

回答

0

檢查用netstat命令來查看您的應用程序是否實際上是綁定到正確的IP /接口:在Linux

> sudo netstat -tunlp | grep 2020 

作品。

你也可以改變這一行:

InetSocketAddress address = new InetSocketAddress(172.22.49.116, 2020); 
+0

netstat -tunlp | grep 2020提供以下輸出:udp 0 0 0.0.0.0:2020 0.0.0.0:* 88352/java – Rumel

+0

我測試了您的代碼。它的工作原理應該如此 – Hossein