简体   繁体   English

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

[英]Connection timed out error in java client-server project

I'm learning some basic java networking stuff and I'm trying to make what I learned come to life, so when I run my server and client classes on the same computer it runs without errors, but when I take the client project to another computer and run the project after running the server it freezes and prints a connection timed out statement. 我正在学习一些基本的Java网络知识,并且试图使我学到的东西变为现实,因此,当我在同一台计算机上运行服务器和客户端类时,它可以正常运行,但是当我将客户端项目转移到另一个计算机上时计算机并在运行服务器后运行项目,它将冻结并打印连接超时语句。

here is my server code 这是我的服务器代码

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);
}
}

and this is my client 这是我的客户

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);
        }

    });

}
}

problem solved 问题解决了

I just had to change the network settings on my server so that it will be discoverable on my network 我只需要更改服务器上的网络设置,即可在网络上发现它

Within the server class you create a server socket on the port 8000, which is fine for both internal and external connections. 在服务器类中,您可以在端口8000上创建一个服务器套接字,这对于内部和外部连接均适用。 However your client class attempts to create a socket with no given IP Address 但是,您的客户端类尝试创建没有给定IP地址的套接字

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

By passing the string "Server ip address", you're essentially telling java to look for a local server, as you did not pass in a proper IP Address. 通过传递字符串“服务器ip地址”,实际上是在告诉Java寻找本地服务器,因为您没有传递正确的IP地址。

So when both classes are running on the same system only the port matters, but you need to identify the IP Address of the server for the client to know where to look for its connection if they're not on the same system. 因此,当两个类都在同一系统上运行时,端口很重要,但是您需要标识服务器的IP地址,以便客户端知道如果它们不在同一系统上,则在哪里寻找其连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM