简单 Java 服务器不工作

Simple Java server not working

我想进入 Java 网络已有一段时间了,根据 Java 网络教程,我创建了自己的服务器和客户端。代码:

package com.gmail.dudewithtude42.projectfilos;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class ProjectFilos {
    public static void main(String[] args) {
        System.out.println("Main Menu\n1. Host Game\n2. Join Game");
        Scanner localScanner=new Scanner(System.in);
        int port;
        inputLoop:while (true){
            String choice=localScanner.nextLine();
            switch (choice){
            case "1":
                System.out.println("What port do you want to use?");
                port=localScanner.nextInt();
                try{
                    System.out.println("Generating socket...");
                    ServerSocket serverSocket = new ServerSocket(port);
                    System.out.println("Connecting socket...");
                    Socket clientSocket=serverSocket.accept();
                    System.out.println("Creating server output...");
                    PrintWriter serverOutput=new PrintWriter(clientSocket.getOutputStream(),true);
                    System.out.println("Creating server input...");
                    BufferedReader serverInput=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                    System.out.println("All systems operational.");
                    String inputLine;
                    while ((inputLine=serverInput.readLine())!=null){
                        serverOutput.println(inputLine);
                        System.out.println(inputLine);
                    }
                    serverSocket.close();
                }catch (IOException serverException) {
                    serverException.printStackTrace();
                }
                break inputLoop;
            case "2":
                System.out.println("What is the IP address of the server?");
                InetAddress serverAddress=null;
                try{
                    serverAddress=InetAddress.getByName(localScanner.nextLine());
                }catch (UnknownHostException ipException) {
                    ipException.printStackTrace();
                }
                System.out.println("What port do you want to connect to?");
                port=localScanner.nextInt();
                try{
                    System.out.println("Generating socket...");
                    Socket connectionSocket=new Socket(serverAddress,port);
                    System.out.println("Creating output...");
                    PrintWriter clientOutput=new PrintWriter(connectionSocket.getOutputStream(),true);
                    System.out.println("Creating server input...");
                    BufferedReader clientInput=new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                    System.out.println("Creating standard/local input...");
                    BufferedReader clientStandardInput=new BufferedReader(new InputStreamReader(System.in));
                    System.out.println("All systems operational.");
                    String userInput;
                    while ((userInput=clientStandardInput.readLine())!=null){
                        clientOutput.println(userInput);
                        System.out.println("echo: "+clientInput.readLine());
                    }
                    connectionSocket.close();
                }catch (IOException clientException){
                    clientException.printStackTrace();
                }
                break inputLoop;
            default:
                break;
            }
        }
        localScanner.close();
    }
}

此代码是一个程序中的服务器和客户端,以供用户访问。但是,我不能完全让它正常工作。代码

-在本地主机上工作正常

-连接到我电脑的私有 IP 时工作正常

-尝试从不同的路由器连接时不起作用

我已经尝试了长途连接的端口转发,使用 TCP/UDP 端口转发到一个端口(本例中为 42424),使用相同的内部和外部起始和结束端口并转发到我的计算机(即 运行 服务器)但无济于事。每次都连接超时,没有其他错误。

我的问题是:我的程序是否存在固有问题,或者这更可能是 router/firewall 问题?如果是防火墙问题,我该如何解决?感谢您的帮助!

要测试是否是配置问题,您要做的第一件事是使用 tcpdump 或 wireshark 进行测试。检查 TCP SYN 消息是否到达服务器,如果是,我们丢弃与端口转发相关的配置问题。 然后检查服务器用 SYN/ACK 响应 SYN 消息。如果没有响应,可能是由于本地防火墙阻止了连接。

不知道是不是客户端尝试连接时超时。它发生在这里吗?

Socket connectionSocket=new Socket(serverAddress,port);

解决此问题后,您必须考虑与服务器的通信:How to read all of Inputstream in Server Socket JAVA