javaFx中的UDP服务器错误
我做了UDP服务器的源代码。代码的任务在通过UDP通信接收数据时如图所示。我使用线程通过UDP通信从其他PC接收数据 我使用另一台PC上的udp测试工具来检查代码 但是,当首先启动我的jar源代码时,udp测试工具无法绑定端口 我的源代码有什么问题? (如果我在其他PC的udp测试工具中设置了不同的值作为绑定端口和发送端口,那么我的源代码成功地显示了该图。)javaFx中的UDP服务器错误,javafx,Javafx,我做了UDP服务器的源代码。代码的任务在通过UDP通信接收数据时如图所示。我使用线程通过UDP通信从其他PC接收数据 我使用另一台PC上的udp测试工具来检查代码 但是,当首先启动我的jar源代码时,udp测试工具无法绑定端口 我的源代码有什么问题? (如果我在其他PC的udp测试工具中设置了不同的值作为绑定端口和发送端口,那么我的源代码成功地显示了该图。) PackageJavaFXApplication15; 导入javafx.application.application; 导入javaf
PackageJavaFXApplication15;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.ButtonBuilder;
导入javafx.scene.layout.FlowPane;
导入javafx.scene.layout.FlowPaneBuilder;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.HBoxBuilder;
导入javafx.scene.layout.StackPane;
导入javafx.scene.layout.VBox;
导入javafx.scene.layout.VBoxBuilder;
导入javafx.stage.stage;
导入javafx.stage.StageBuilder;
导入java.io.*;
导入java.net。*;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.application.Platform;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.concurrent.ScheduledService;
导入javafx.concurrent.Service;
导入javafx.concurrent.Task;
导入javafx.geometry.Pos;
导入javafx.scene.chart.LineChart;
导入javafx.scene.chart.NumberAxis;
导入javafx.scene.chart.XYChart;
导入javafx.scene.control.ComboBox;
导入javafx.scene.control.Label;
导入javafx.scene.control.LabelBuilder;
导入javafx.scene.control.ProgressIndicator;
导入javafx.scene.control.TextField;
导入javafx.scene.control.TextFieldBuilder;
导入javafx.scene.layout.GridPane;
导入javafx.scene.text.Font;
导入javafx.scene.text.fontpostation;
导入javafx.scene.text.TextAlignment;
导入javafx.util.Duration;
导入sun.misc.hextumpEncoder;
公共类JavaFXApplication15扩展了应用程序{
公共数据采集器接收插座;
公共数据包接收包;
公共DatagramSocket sendsocket;
公共数据包发送包;
公共InetSocketAddress远程地址;
公共标签;
公共标签;
公共标签(Label_IP_in);;
公共文本字段tx_IP;
公共标签发送端口;
公共标签标签\发送端口\输入;
公共标签接收端口;
公共标签标签接收端口输入;
公共标签状态;
公共文本字段tx_发送端口;
公共文本字段tx_RECEIVEPORT;
公共按钮bt_公司;
公共按钮bt1;
公共按钮bt2;
公共按钮bt_播放;
公共可观察列表play_num=null;
公共组合框组合框;
私有字符串消息;
私有XYChart.系列;
私人时间计时器;
私人发展指标;
私有静态字符串s=null;
私有静态字符串IP=“192.168.101.30”;
专用静态整数RECEIVEPORT=8084;
公共双时间计数器=0.0;
公共双扭矩接收值=0.0;
公共字节[]扭矩×十六进制;
私有字符串文本;
专用字节[]buf;
@凌驾
public void start(Stage)引发异常{
/*计时器*/
计时器=新的时间线(新的关键帧(Duration.millis(1000),新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
时间计数器=时间计数器+1;
}
}));
timer.setCycleCount(Timeline.unfinite);
timer.play();
/*身材*/
阶段。设置标题(“折线图样本”);
//定义轴
最终数字axis xAxis=新数字axis();
最终数字axis yAxis=新数字axis();
xAxis.setLabel(“时间]”;
yAxis.setLabel(“强制[N]”);
//创建图表
最终线形图线形图=
新的线形图(xAxis、yAxis);
线形图。设置标题(“接收数据”);
//定义系列
series=新的XYChart.series();
系列。设置名称(“扭矩”);
series.getData().add(新的XYChart.Data(0.0,0.0));
lineChart.getData().add(系列);
VBox root=VBoxBuilder.create().spating(25).children(lineChart.build();
场景=新场景(根);
接收UDP();
stage=StageBuilder.create().width(640).height(640).scene(scene).title(“”.build();
stage.show();
}
私有void receive_UDP()抛出SocketException,IOException{
ScheduledService ss=新的ScheduledService()
{
@凌驾
受保护的任务createTask()
{
任务=新任务()
{
@凌驾
受保护的布尔调用()引发异常
{
receivesocket=null;
字节[]接收缓冲区=新字节[1024];
receivepacket=新数据包(receiveBuffer,receiveBuffer.length);
receivesocket=新的DatagramSocket(RECEIVEPORT);
receivesocket.receive(receivepacket);
message=新字符串(receivepacket.getData(),0,receivepacket.getLength());
torque_Hex=receivepacket.getData();
System.out.println(消息);
如果(消息!=“状态:等待”){
Platform.runLater(()->label_status.setText(“status:Done”);
/*显示数字*/
Platform.runLater(()->series.getData().add(新的XYChart.Data)(时间计数器,Double.parseDouble(m
package javafxapplication15;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.FlowPaneBuilder;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.stage.Stage;
import javafx.stage.StageBuilder;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.LabelBuilder;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFieldBuilder;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.TextAlignment;
import javafx.util.Duration;
import sun.misc.HexDumpEncoder;
public class JavaFXApplication15 extends Application {
public DatagramSocket receivesocket;
public DatagramPacket receivepacket;
public DatagramSocket sendsocket;
public DatagramPacket sendpacket;
public InetSocketAddress remoteAdress;
public Label label;
public Label label_IP;
public Label label_IP_in;
public TextField tx_IP;
public Label label_SENDPORT;
public Label label_SENDPORT_in;
public Label label_RECEIVEPORT;
public Label label_RECEIVEPORT_in;
public Label label_status;
public TextField tx_SENDPORT;
public TextField tx_RECEIVEPORT;
public Button bt_co;
public Button bt1;
public Button bt2;
public Button bt_play ;
public ObservableList<String> play_num=null;
public ComboBox<String> comboBox;
private String message;
private XYChart.Series series;
private Timeline timer;
private ProgressIndicator indicator;
private static String s = null;
private static String IP = "192.168.101.30";
private static Integer RECEIVEPORT = 8084;
public double time_counter=0.0;
public double torque_receive_value=0.0;
public byte[] torque_Hex;
private String text;
private byte[] buf;
@Override
public void start(Stage stage) throws Exception{
/* timer */
timer = new Timeline(new KeyFrame(Duration.millis(1000), new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
time_counter = time_counter+1;
}
}));
timer.setCycleCount(Timeline.INDEFINITE);
timer.play();
/* Figure*/
stage.setTitle("Line Chart Sample");
//defining the axes
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("Receive Data");
//defining a series
series = new XYChart.Series();
series.setName("Torque");
series.getData().add(new XYChart.Data(0.0,0.0));
lineChart.getData().add(series);
VBox root = VBoxBuilder.create().spacing(25).children(lineChart).build();
Scene scene = new Scene(root);
recieve_UDP();
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());
torque_Hex = receivepacket.getData();
System.out.println(message);
if(message != "Status:Wait"){
Platform.runLater( () ->label_status.setText("Status:Done"));
/* show Figure */
Platform.runLater( () -> series.getData().add(new XYChart.Data(time_counter,Double.parseDouble( message))));
}
receivesocket.close();
return true;
};
};
return task;
}
};
ss.start();
}
public static void main(String[] args) {
launch(args);
}
}