简体   繁体   English

简单的Java服务器无法正常工作

[英]Simple Java server not working

I have wanted to get into Java networking for some time now and, based on the Java networking tutorial, I have created my own server and client. 我已经想进入Java网络一段时间了,根据Java网络教程,我已经创建了自己的服务器和客户端。 Code: 码:

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

This code is both the server and the client in one program for user accessibility. 此代码在一个程序中既是服务器又是客户端,以便用户访问。 However, I cannot quite get it to work properly. 但是,我不能完全使它正常工作。 The code 编码

-Works fine on localhost -在本地主机上工作正常

-Works fine when connecting to the private IP of my computer -连接到我的计算机的专用IP时工作正常

-Doesn't work when trying to connect from a different router -尝试从其他路由器连接时不起作用

I have tried port forwarding for the long-distance connection, using a TCP/UDP port forward over one port (42424 in this case), using the same internal and external starting and ending port and forwarding to my computer (which is running the server) but to no avail. 我已经尝试过长距离连接的端口转发,即使用一个端口上的TCP / UDP端口转发(在这种情况下为42424),使用相同的内部和外部起始端口和结束端口,然后转发到我的计算机(正在运行服务器) ),但无济于事。 Every time the connection times out, no other errors. 每次连接超时,都不会发生其他错误。

My question is: is there something inherently wrong with my program, or is this more likely a router/firewall problem? 我的问题是:我的程序天生有问题,还是更有可能是路由器/防火墙问题? And if it is a firewall problem, how would I fix it? 如果是防火墙问题,我该如何解决? Any help is appreciated! 任何帮助表示赞赏!

To test if it is a configuration problem the first thing you have to do is to test with tcpdump or wireshark. 要测试它是否是配置问题,您要做的第一件事就是使用tcpdump或wirehark进行测试。 Check if a TCP SYN message is arriving to the server, if it is, we discard a configuration problem related to port forwarding. 检查是否有TCP SYN消息到达服务器,如果是,我们将丢弃与端口转发有关的配置问题。 Then check the SYN message is responded with a SYN/ACK from the server. 然后检查来自服务器的SYN / ACK是否响应了SYN消息。 If it is not responded it could be due to local firewall blocking connections. 如果未响应,则可能是由于本地防火墙阻止了连接。

I don't know if the timeout is when the client tries to connect. 我不知道是否超时是客户端尝试连接时。 Is it happening here? 这是发生在这里吗?

Socket connectionSocket=new Socket(serverAddress,port);

After you solve this problem you must consider this for the communication with the server: How to read all of Inputstream in Server Socket JAVA 解决此问题后,必须考虑与服务器的通信: 如何读取服务器套接字JAVA中的所有Inputstream

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

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