Reputation: 1
For historical reasons I still use Netbeans 12.5 with several Java/JavaFx projects. I have a e-car charger connectet to my LAN via a switch. I have opened the necessary ports so by Using UDP Test Tool 3.0 I can send and receive UDP messages to/from the charger without problems. It also works with my Java app when I build a .jar and exexutes it from a command window (DOS prompt). So far so good!
But when I run the same code in the debugger in Netbeans 12.5 IDE, the incoming messages are blocked somehow. They never reach my message queue. I have tried to play around with permissions in Windows Defender / Firewall (no 3.rd party antivirus installed). But no success.
I have googled and searched in StackOverflow, but can't find any similar postings. Does anybody have a suggestion to what it can be and how I can fix it?
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/javafx/FXMain.java to edit this template
*/
package listentest2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ListenTest2 extends Application {
private Thread listenThread;
Task<Integer> chargerListenerTask = new Task<Integer>()
{
int noOfMessages = 0;
@Override
protected Integer call() throws Exception
{
updateMessage("Listener thread started");
while (true)
{
if (isCancelled())
{
updateMessage("Listener thread cancelled");
break;
}
try
{
// Constructs a datagram socket and binds it to the specified port on the local host machine..
DatagramSocket socket = new DatagramSocket(7090);
System.out.println("UDP listener started on port 7090");
// Buffer to hold incoming datagrams
byte[] buffer = new byte[1024];
while (true)
{
// Create a DatagramPacket to hold the incoming datagram
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// Receive the incoming datagram
socket.receive(packet);
// Extract the message from the datagram
String message = new String(packet.getData(), 0, packet.getLength());
InetAddress chargerAddress = packet.getAddress();
int senderPort = packet.getPort();
// Print out the received message and sender's address
System.out.printf("Received message: \"%s\" from %s:%d%n", message, chargerAddress, senderPort);
}
}
catch (IOException e)
{
}
updateMessage("Listener thread: Listen terminated");
}
updateMessage("Listener thread finished");
return 0;
}
};
@Override
public void start(Stage primaryStage)
{
listenThread = new Thread(chargerListenerTask);
listenThread.setDaemon(true);
listenThread.start();
Button btn = new Button();
btn.setText("Say 'Hello Listener'");
btn.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out.println("Hello Listener!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello Listener!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Upvotes: -1
Views: 46