gpt4 book ai didi

Java - UDP 和组播检测

转载 作者:行者123 更新时间:2023-12-02 07:22:52 25 4
gpt4 key购买 nike

在单个端口上,我想监听多播、UDP 和 TCP 流量。 (在我的局域网上)如果检测到某些内容,我还想通过 UDP 进行响应。

代码如下,但仅用于多播检测。 while(true) 循环肯定是在 main() 中执行此操作。

但是我在添加另一种协议(protocol)检测方法时遇到了困难。单个应用程序可以在多种协议(protocol)中打开多个套接字吗?我确信这是我对线程的了解有限,但也许有人可以在下面看到我的问题。

public class LANPortSniffer  {

private static void autoSendResponse() throws IOException {

String sentenceToSend = "I've detected your traffic";
int PortNum = 1234;

DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("192.168.1.121");
byte[] sendData = new byte[1024];

sendData = sentenceToSend.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, PortNum);
clientSocket.send(sendPacket);
clientSocket.close();


}//eof autoSendResponse


private static void MulticastListener() throws UnknownHostException {

final InetAddress group = InetAddress.getByName("224.0.0.0");
final int port = 1234;

try {

System.out.println("multi-cast listener is started......");
MulticastSocket socket = new MulticastSocket(port);
socket.setInterface(InetAddress.getLocalHost());
socket.joinGroup(group);

byte[] buffer = new byte[10*1024];

DatagramPacket data = new DatagramPacket(buffer, buffer.length);

while (true) {
socket.receive(data);

// auto-send response
autoSendResponse();

}


} catch (IOException e) {
System.out.println(e.toString());
}

}//eof MulticastListener

// this method is not even getting launched
private static void UDPListener() throws Exception {

DatagramSocket serverSocket = new DatagramSocket(1234);
byte[] receiveData = new byte[1024];

System.out.println("UDP listener is started......");
while(true)
{
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData());
System.out.println("UDP RECEIVED: " + sentence);

}

}



public static void main(String[] args) throws Exception {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});


try {
MulticastListener();

} catch (UnknownHostException e) {

e.printStackTrace();
}


// this does not appear to be detected:
try {
UDPListener();

} catch (Exception e) {

e.printStackTrace();
}


}
}//eof LANPortSniffer

在 main() 中,我尝试添加第二个 try/catch,以实现简单的 UDPListener() 方法。
但当我在 Eclipse 中运行该应用程序时,它似乎被忽略了。

main()方法是否只允许一次try/catch?

简而言之,我想同时在单个端口上监听多播、UDP 和 TCP 数据包。这可能吗?

最佳答案

您在这里遇到了线程问题。我认为你需要加深对 Java 的理解。当您调用 MulticastListener() 时,它永远不会离开该 block ,直到连接失败。它有一个连续的 while 循环。您需要为每个 Activity 创建一个新线程。

Thread t = new Thread(new Runnable() { 
public void run() {
MulticastListener();
}
}
t.start();

但是,我建议您在开始尝试实现线程程序之前,先阅读对程序流程的理解以及更多面向对象方法的使用。

关于Java - UDP 和组播检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com