Java客户端-服务器项目中的连接超时错误

时间:2018-07-09 19:23:39

标签: java networking client-server

我正在学习一些基本的Java网络知识,并且试图使我学到的东西变为现实,因此,当我在同一台计算机上运行服务器和客户端类时,它可以正常运行,但是当我使用客户端项目转移到另一台计算机,并在运行服务器后运行该项目,该服务器将冻结并打印连接超时语句。

这是我的服务器代码

package sample;

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;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        TextArea ta = new TextArea();
        primaryStage.setTitle("server");
        primaryStage.setScene(new Scene(new ScrollPane(ta), 450, 200));
        primaryStage.show();

        new Thread(()->{
           try {
               ServerSocket ss = new ServerSocket(8000);
               Platform.runLater(() ->
                       ta.appendText("Server started at " + new Date() + '\n'));

           Socket s = ss.accept();

           DataInputStream inputFromClient = new DataInputStream(s.getInputStream());
           DataOutputStream outputToClient = new DataOutputStream(s.getOutputStream());

           while (true) {
               double radius = inputFromClient.readDouble();

               double area = radius * radius * Math.PI;
               outputToClient.writeDouble(area);

               Platform.runLater(() -> {
                   ta.appendText("Radius received from client: "
                           + radius + '\n');
                   ta.appendText("Area is: " + area + '\n');
                   });
           }

       } catch (Exception e){
            e.printStackTrace();
       }
    }).start();
    }



public static void main(String[] args) {
    launch(args);
}
}

这是我的客户

package sample;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Main extends Application {

DataOutputStream toServer = null;
DataInputStream fromServer = null;

public void start(Stage primaryStage) {

    BorderPane pane = new BorderPane();
    pane.setPadding(new Insets(5, 5, 5, 5));
    pane.setStyle("-fx-border-color: green");
    pane.setLeft(new Label("Enter a radius: "));

    TextField tf = new TextField();
    tf.setAlignment(Pos.BOTTOM_RIGHT);
    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"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
    tf.setOnAction(e -> {

        try {
            Socket socket = new Socket("server IP address", 8000);
            fromServer = new DataInputStream(socket.getInputStream());
            toServer = new DataOutputStream(socket.getOutputStream());
        } catch (IOException ex) {
            ta.appendText(ex.toString() + '\n');
        }

        try {
            double radius = Double.parseDouble(tf.getText().trim());

            toServer.writeDouble(radius);
            toServer.flush();

            double area = fromServer.readDouble();

            ta.appendText("Radius is " + radius + "\n");
            ta.appendText("Area received from the server is "
                    + area + '\n');
        } catch (IOException ex) {
            System.err.println(ex);
        }

    });

}
}

2 个答案:

答案 0 :(得分:0)

问题解决了

我只需要更改服务器上的网络设置,即可在我的网络上发现它

答案 1 :(得分:-1)

在服务器类中,您可以在端口8000上创建一个服务器套接字,这对于内部和外部连接均适用。但是,您的客户端类会尝试创建一个没有给定IP地址的套接字

Socket socket = new Socket("server IP address", 8000);

通过传递字符串“服务器ip地址”,实际上是在告诉java寻找本地服务器,因为您没有传递正确的IP地址。

因此,当两个类都在同一系统上运行时,只有端口很重要,但是您需要标识服务器的IP地址,以便客户端知道如果它们不在同一系统上,则在哪里寻找其连接。