一、服務器端
ServerSocket serverSocket = new ServerSocket(8080);//創建服務器套接字,
Socket socket = serverSocket.accept();//並等待連接
二、客戶端
Socket socket = new Socket("localhost",8080);//創建套接字並通過該套接字向服務器發送請求連接
客戶端代碼:
public class Client extends Application{
//初始化駛入輸出流
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane pane = new BorderPane();
pane.setPadding(new Insets(10,10,10,10));
pane.setLeft(new Label("輸入半徑: "));
TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_LEFT);
pane.setCenter(tf);
BorderPane mainPane = new BorderPane();
TextArea ta = new TextArea();
mainPane.setCenter(new ScrollPane(ta));
mainPane.setTop(pane);
Scene scene = new Scene(mainPane,450,200);
primaryStage.setTitle("Client");
primaryStage.setScene(scene);
primaryStage.show();
try {
Socket socket = new Socket("localhost",8080);
inputStream = new DataInputStream(socket.getInputStream());
outputStream = new DataOutputStream(socket.getOutputStream());
}catch (IOException e){
System.err.println(e);
}
tf.setOnAction(e->{
try{
double radius = Double.parseDouble(tf.getText().trim());
outputStream.writeDouble(radius);
outputStream.flush();
double area = inputStream.readDouble();
ta.appendText("半徑: "+radius+"\n");
ta.appendText("從服務器得到的面積: "+area+"\n");
}catch (IOException ex){
ta.appendText(ex.toString()+"\n");
}
});
}
}
服務器端代碼:
public class MultiThreadServer extends Application{
private TextArea ta = new TextArea();
private int clientNo = 0;
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(new ScrollPane(ta),450,200);
primaryStage.setTitle("Server");
primaryStage.setScene(scene);
primaryStage.show();
new Thread(()->{//ServerSocket accept()方法執行的時候花費時間,不應放在JavaFX程序線程中運行,因此放在一個獨立的線程中
try{
//服務器套接字
ServerSocket serverSocket = new ServerSocket(8080);
ta.appendText("服務器開始工作: "+new Date().toString()+"\n");
while (true){
//等待連接
Socket socket = serverSocket.accept();
clientNo++;
Platform.runLater(()->{
ta.appendText("第 "+clientNo+" 個客戶端連接成功: "+new Date().toString()+"\n");
InetAddress inetAddress = socket.getInetAddress();
ta.appendText("客戶端"+clientNo+"的主機名爲:"+inetAddress.getHostName()+"\n");
ta.appendText("客戶端"+clientNo+"的地址爲:"+inetAddress.getHostAddress()+"\n");
});
//創建一個新線程處理通信
new Thread(new HandleAClient(socket)).start();
}
}catch (IOException e){
e.printStackTrace();
}
}).start();
}
class HandleAClient implements Runnable{
private Socket socket;
public HandleAClient(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
//創建輸入輸出流接受客戶端的數據
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
while (true){
double radius = inputStream.readDouble();
double area = radius*radius*Math.PI;
outputStream.writeDouble(area);
Platform.runLater(()->{
ta.appendText("客戶端發過來的半徑:"+radius+"\n");
ta.appendText("服務器發出去的面積:"+area+"\n");
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}