javafx如何正确地使客户机-服务器用java进行对话?
import java.io.*;
import java.net.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Client extends Application {
static DataOutputStream toServer = null;
static DataInputStream fromServer = null;
public TextField field;
public static TextArea area;
public BorderPane mainPane;
static Button topLeft;
static Button topMid;
static Button topRight;
static Button midLeft;
static Button midMid;
static Button midRight;
static Button botLeft;
static Button botMid;
static Button botRight;
private BorderPane borderPane;
@Override
public void start(Stage primaryStage) throws Exception {
// UI
BorderPane fieldPane = new BorderPane();
fieldPane.setPadding(new Insets(5, 5, 5, 5));
fieldPane.setLeft(new Label("Enter text: "));
field = new TextField();
field.setAlignment(Pos.BOTTOM_RIGHT);
fieldPane.setCenter(field);
BorderPane mainPane = new BorderPane();
area = new TextArea();
area.setEditable(false);
mainPane.setCenter(new ScrollPane(area));
mainPane.setTop(fieldPane);
TextArea output = new TextArea();
output.setWrapText(true);
GridPane gamePane = new GridPane();
gamePane.addRow(2);
gamePane.addColumn(2);
gamePane.setHgap(250);
gamePane.setVgap(250);
topLeft = new Button();
topLeft.setMinSize(60, 60);
topMid = new Button();
topMid.setMinSize(60, 60);
topRight = new Button();
topRight.setMinSize(60, 60);
midLeft = new Button();
midLeft.setMinSize(60, 60);
midMid = new Button();
midMid.setMinSize(60, 60);
midRight = new Button();
midRight.setMinSize(60, 60);
botLeft = new Button();
botLeft.setMinSize(60, 60);
botMid = new Button();
botMid.setMinSize(60, 60);
botRight = new Button();
botRight.setMinSize(60, 60);
gamePane.add(topLeft, 0, 0);
gamePane.add(topMid, 0, 1);
gamePane.add(topRight, 0, 2);
gamePane.add(midLeft, 1, 0);
gamePane.add(midMid, 1, 1);
gamePane.add(midRight, 1, 2);
gamePane.add(botLeft, 2, 0);
gamePane.add(botMid, 2, 1);
gamePane.add(botRight, 2, 2);
mainPane.setCenter(gamePane);
mainPane.setTop(output);
// Create a serverScene and place it in the stage
output.setText("Server Started");
Scene scene = new Scene(mainPane, 450, 200);
primaryStage.setTitle("Client");
primaryStage.setScene(scene);
primaryStage.show();
// END UI
try { // start connect
Socket socket = new Socket("localhost", 8182);
fromServer = new DataInputStream(socket.getInputStream());
toServer = new DataOutputStream(socket.getOutputStream());
} catch (Exception e) {
area.appendText(e.toString() + '\n');
}
field.setOnAction(e -> {
try {/*
String normal = this.field.getText();
toServer.writeUTF(normal);
toServer.flush();
String inverse = fromServer.readUTF();
area.appendText("Inverse from the server is: " + inverse + '\n');*/
} catch (Exception e2) {
area.appendText(e2.toString() + '\n');
}
});
topLeft.setOnAction(e -> {
try {
try {
sendMove.send(1);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
} catch (IOException ioException) {
area.appendText(ioException.toString() + '\n');
}
});
topMid.setOnAction(e -> {
try {
try {
sendMove.send(2);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
});
topRight.setOnAction(e -> {
try {
try {
sendMove.send(3);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
});
midLeft.setOnAction(e -> {
try {
try {
sendMove.send(4);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
});
midMid.setOnAction(e -> {
try {
try {
sendMove.send(5);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
});
midRight.setOnAction(e -> {
try {
try {
sendMove.send(6);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
});
botLeft.setOnAction(e -> {
try {
try {
sendMove.send(7);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
});
botMid.setOnAction(e -> {
try {
try {
sendMove.send(8);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
});
botRight.setOnAction(e -> {
try {
try {
sendMove.send(9);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
});
}
public static void main(String[] args) {
launch(args);
}
}
class sendMove{
public static void send(int i) throws IOException, InterruptedException{
Client.toServer.write(i);
Client.toServer.flush();
Platform.runLater(()->{
int inverse=0;
try{
inverse=Client.fromServer.readInt();
}catch(IOException e){
e.printStackTrace();
}
Client.area.appendText(inverse+" recieved back");
});
Thread.sleep(100);
}
}
import java.io.*;
import java.net.*;
import java.util.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class Server extends Application {
private int clientNo = 0;
private TextArea ta = new TextArea();
@Override
public void start (Stage primaryStage) throws Exception {
ta.setEditable(false);
Scene scene = new Scene(new ScrollPane(ta), 450, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("Server");
primaryStage.show();
new Thread( () -> {
try {
ServerSocket serverSocket = new ServerSocket(8182);
Platform.runLater( () -> {
ta.appendText("Server started time " + new Date() + '\n');
});
while (true) {
Socket socket = serverSocket.accept();
clientNo++;
Platform.runLater( () -> {
ta.appendText("Starting client " + clientNo + " at " + new Date() + '\n');
ta.appendText("Client " + clientNo + " IP Address is " + socket.getInetAddress().getHostAddress() + '\n');
});
new Thread(new ThreadClient(socket)).start();
}
} catch (Exception e) {
ta.appendText(e.toString() + '\n');
}
}).start();
}
class ThreadClient implements Runnable {
private Socket socket;
public ThreadClient(Socket socket) {
this.socket = socket;
}
@Override
public void run () {
try {
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
while (true) {
int normal = inputFromClient.readInt();
int inverse = normal;
outputToClient.write(inverse);
Platform.runLater( () -> {
ta.appendText("Text received from client: " + normal + '\n');
ta.appendText("Inverse is " + inverse + '\n');
});
}
} catch (Exception e) {
ta.appendText(e.toString() + '\n');
}
}
}
public static void main (String[] args) {
Application.launch(args);
}
}
下面是两段代码
问题:
单击按钮将完全冻结客户端,不会出现错误
总体思路:
是点击一个按钮,发送到服务器,服务器发送回两个客户端。客户端使用数字来禁用相应的按钮,添加一些文本和编辑,并发出砰砰声、抽搐声
共 (0) 个答案