I have trouble coding UDP communication in JavaFx.
The code is capable of sending message but is not capable of receiving message.
What I should revise in the source code to receive the message?
Here is the source code:
public class JavaFXApplication10 extends Application {
public DatagramSocket receivesocket;
public DatagramPacket receivepacket;
public DatagramSocket sendsocket;
public DatagramPacket sendpacket;
public InetSocketAddress remoteAdress;
public Label label_IP;
public TextField tx_IP;
public Label label_SENDPORT;
public Label label_RECEIVEPORT;
public TextField tx_SENDPORT;
public TextField tx_RECEIVEPORT;
public Button bt_co;
public Button bt_start ;
private String message;
private XYChart.Series series;
private Timeline timer;
private static String IP = "192.168.121.23";
private static Integer SENDPORT = 12345;
private static Integer RECEIVEPORT = 12345;
public double time_counter=0.0;
private String text;
private byte[] b;
@Override
public void start(Stage stage) throws Exception{
/* text */
tx_IP = TextFieldBuilder.create().text(IP).build();
/* text */
tx_SENDPORT = TextFieldBuilder.create().text(""+SENDPORT).build();
/* text */
tx_RECEIVEPORT = TextFieldBuilder.create().text(""+RECEIVEPORT).build();
/*button */
bt_co = ButtonBuilder.create().text("Connection")
.prefWidth(200)
.alignment(Pos.CENTER)
.id("connect")
.build();
/* button_start */
bt_start = ButtonBuilder.create().text("START")
.id("start")
.build();
/* timer */
timer = new Timeline(new KeyFrame(Duration.millis(1000), new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
time_counter = time_counter+1; // time
}
}));
timer.setCycleCount(Timeline.INDEFINITE);
timer.play();
/*figure*/
stage.setTitle("Line Chart Sample");
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Time [s]");
yAxis.setLabel("Force [N]");
//creating the chart
final LineChart<Number,Number> lineChart =
new LineChart<Number,Number>(xAxis,yAxis);
lineChart.setTitle("");
//defining a series
series = new XYChart.Series();
series.setName("Force");
series.getData().add(new XYChart.Data(0.0,0.0));
lineChart.getData().add(series);
HBox root1 = HBoxBuilder.create().spacing(100).children(tx_IP ,tx_SENDPORT,tx_RECEIVEPORT).build();
HBox root2 = HBoxBuilder.create().spacing(50).children(bt_co).build();
HBox root3 = HBoxBuilder.create().spacing(25).children(bt_start).build();
VBox root4 = VBoxBuilder.create().spacing(25).children(root1,root2,root3,lineChart).build();
Scene scene = new Scene(root4);
recieve_UDP();
scene.addEventHandler(ActionEvent.ACTION,actionHandler);
stage = StageBuilder.create().width(640).height(640).scene(scene).title(" ").build();
stage.show();
}
private void recieve_UDP() throws SocketException, IOException {
ScheduledService<Boolean> ss = new ScheduledService<Boolean>()
{
@Override
protected Task<Boolean> createTask()
{
Task<Boolean> task = new Task<Boolean>()
{
@Override
protected Boolean call() throws Exception
{
receivesocket = null;
byte[] receiveBuffer = new byte[1024];
receivepacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
receivesocket = new DatagramSocket(RECEIVEPORT);
receivesocket.receive(receivepacket);
message = new String(receivepacket.getData(),0, receivepacket.getLength());
System.out.println(message);
receivesocket.close();
return true;
};
};
return task;
}
};
ss.start();
}
EventHandler<ActionEvent> actionHandler = new EventHandler<ActionEvent>(){
public void handle (ActionEvent e){
//////////////////////////////////////////////////////////////////////////////
Button src =(Button)e.getTarget();
text = src.getId();
System.out.println(text);
b = new byte[5];
if(text == "connect"){
String text_IP = tx_IP.getText();
label_IP.setText(text_IP);
IP = text_IP;
String text_SENDPORT = tx_SENDPORT.getText();
label_SENDPORT.setText(text_SENDPORT);
SENDPORT = Integer.parseInt( text_SENDPORT);
String text_RECEIVEPORT = tx_RECEIVEPORT.getText();
label_RECEIVEPORT.setText(text_RECEIVEPORT);
RECEIVEPORT = Integer.parseInt(text_RECEIVEPORT);
}
else{
remoteAdress = new InetSocketAddress(IP, SENDPORT);
sendsocket = null;
try {
sendsocket = new DatagramSocket();
} catch (SocketException ex) {
Logger.getLogger(JavaFXApplication10.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(text=="start"){
b[0] = (byte)0x02;
text ="OK";
}
else{
}
Send_UDP();
///////////////////////////////////////////////////////
}
};
public void Send_UDP(){
///////////////////////////////////////////////////////
if(text=="OK"){
sendpacket = new DatagramPacket(b, b.length,remoteAdress);
try {
sendsocket.send(sendpacket);
} catch (IOException ex) {
Logger.getLogger(JavaFXApplication10.class.getName()).log(Level.SEVERE, null, ex);
}
sendsocket.close();
text="";
}
else {}
///////////////////////////////////////////////////////
}
public static void main(String[] args) {
launch(args);
}
}