我必须用GUI创建Java程序,它只是服务器和客户端之间的文本聊天。我已经提到了我教授的Powerpoint演示文稿并复制了客户端键入圆半径的服务器和客户端程序的示例代码,服务器自动计算并返回客户端的圆形区域(示例代码没有输入的文本字段) )。我已将示例代码更改为代码,服务器和客户端可以在文本中相互聊天。我的客户端类工作,我不需要更改Client类中的任何内容,除了输入的文本保持为字符串而不是双精度。
我测试了我的服务器。为什么我的服务器在输入简单文本后按Enter键时会冻结?
服务器:
var promiseWrapper = (xhr, d) => new Promise(resolve => xhr(d, (p) => resolve(p)))
Promise.all([promiseWrapper(d3.json,"yemen.json"),promiseWrapper(d3.csv,"droneData.csv")]).then(resolve =>{
createMap(resolve[0],resolve[1])
})
function createMap(districts,drones){...visualization code...
客户端
import java.io.*;
import java.net.*;
import javafx.geometry.Insets;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
public class Server extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Text area for displaying contents
BorderPane paneForTextField = new BorderPane();
paneForTextField.setPadding(new Insets(5, 5, 5, 5));
paneForTextField.setStyle("-fx-border-color: green");
paneForTextField.setLeft(new Label("Type here: "));
TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_RIGHT);
paneForTextField.setCenter(tf);
BorderPane mainPane = new BorderPane();
TextArea ta = new TextArea();
mainPane.setCenter(new ScrollPane(ta));
mainPane.setTop(paneForTextField);
// Create a scene and place it in the stage
Scene scene = new Scene(mainPane, 450, 200);
primaryStage.setTitle("Server"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
try {
Socket socket = new Socket("localhost", 8000);
// Socket socket = new Socket("130.254.204.36", 8000);
// Socket socket = new Socket("drake.Armstrong.edu", 8000);
// Create an input stream to receive data from the server
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
// Create an output stream to send data to the server
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
// Get the radius from the text field
String serverText = tf.getText();
// Send the radius to the server
outputToClient.writeBytes(serverText);
outputToClient.flush();
// Get area from the server
String clientText = inputFromClient.readLine();
// Display to the text area
ta.appendText("Client: " + clientText + "\n");
ta.appendText("Server: " + serverText + '\n');
}
catch (IOException ex) {
System.err.println(ex.toString() + '\n');
}
tf.setOnAction(e -> {
new Thread( () -> {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
Platform.runLater(() ->
ta.appendText("Server is working" + '\n'));
// Listen for a connection request
Socket socket = serverSocket.accept();
// Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(
socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(
socket.getOutputStream());
while (true) {
// Receive radius from the client
String clientText = inputFromClient.readLine();
String serverText = tf.getText();
// Compute area
// Send area back to the client
outputToClient.writeBytes("Server: " + serverText);
Platform.runLater(() -> {
ta.appendText("Client: " + clientText + '\n');
ta.appendText("Server: " + serverText + '\n');
});
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}).start();
});
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}