简体   繁体   English

Java网络服务器 - 客户端错误

[英]Java networking server-client error

I'm trying to program a (simple, for starters) server-client thingy, just to establish a connection and see if it works. 我正在尝试为(简单的,对于初学者)服务器客户端编程,只是为了建立连接并查看它是否有效。 And it does. 确实如此。 As long as I stay inside my own four walls/network. 只要我留在自己的四面墙/网络内。 As soon as I try to go via my routers IP the client produces a mighty fine error message. 一旦我尝试通过我的路由器IP,客户端就会产生一个强大的错误消息。 All the usual suspects have been eliminated: Router Port forwarding is on, the firewall doesn't interfere (well, it still doesn't work when I turn it off), and canyouseeme.org says that it can establish a connection to my chosen port when the server runs. 所有常见的嫌疑人都已被淘汰:路由器端口转发已打开,防火墙不会干扰(嗯,当我关闭它时它仍然无法工作),并且canyouseeme.org说它可以建立与我选择的连接服务器运行时的端口。

Here is the source code for the server, since I figured out it was possible to just go via the command line with a little telnetting. 这是服务器的源代码,因为我发现可以通过一点telnet来通过命令行。 When I try to establish a connection, it just says Could not open connection to the host, on port 49163:Connection failed 当我尝试建立连接时,它只是说Could not open connection to the host, on port 49163:Connection failed

Server: 服务器:

import java.net.*;
import java.io.*;

public class ChatServer {
    public static void main(String[] args) throws IOException {

        ServerSocket server = null;

        try {
            System.setProperty("java.net.preferIPv4Stack" , "true");
            server = new ServerSocket(49163);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 49163.");
            System.exit(1);
        }

        Socket client = null;
        try {
            client = server.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(client.getOutputStream(), true);
        BufferedReader in = new BufferedReader(newInputStreamReader(client.getInputStream()));
        String inputLine;
        String outputLine;

        out.println("Connection established");
        while ((inputLine = in.readLine()) != null) {
            if (inputLine.equals("exit")) {
                break;
            }
        outputLine = inputLine;
        out.println(outputLine);
        }
        out.close();
        in.close();
        client.close();
        server.close();
        System.out.println("Server offline");
    }
}

Oh yeah, preferIP4vStack was something I picked up on Stackoverflow, but it doesn't seem to work for me. 哦,是的,我更喜欢在Stackoverflow上使用的IP4vStack,但它似乎对我不起作用。

For some reason I'm not able to inlcude the code for the client, I keep getting messages about formatting, and I just can't figure it out. 出于某种原因,我无法包含客户端的代码,我不断收到有关格式化的消息,我只是想不出来。 But the way I see it is that I'm not even able to connect via the command line (when I try to connect to the "real" IP), so the client doesn't have to get involved. 但我看到它的方式是我甚至无法通过命令行连接(当我尝试连接到“真正的”IP时),因此客户端不必参与。 But for you who want to see the error anyway: 但对于想要查看错误的人:

 java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.<init>(Unknown Source) at java.net.Socket.<init>(Unknown Source) at ChatClient.main(ChatClient.java:12) 

Could not open connection to the host, on port 49163:Connection failed 无法在端口49163上打开与主机的连接:连接失败

No it didn't. 不,没有。 Thats not a Java message. 这不是Java消息。 Your code said that, when you caught a ConnectException that had a much more detailed message of its own. 你的代码说,当你发现一个ConnectException,它有一个更详细的消息。 Print that and you might have some hope of finding what went wrong. 打印出来,你可能有希望找到问题所在。 Making up your own messages and suppressing what the exception tells you is rarely if ever a good idea. 编写自己的消息并抑制异常告诉您的内容很少是一个好主意。

The message will probably be one of: 该消息可能是以下之一:

  • 'connection refused', which means you got the target host or port wrong, or there is a firewall '连接被拒绝',这意味着你得到了目标主机或端口错误,或者有防火墙
  • 'connection timed out', which is a network connectivity problem '连接超时',这是一个网络连接问题
  • 'no route to host', which is an IP routing problem '无路由到主机',这是一个IP路由问题
  • 'unknown host', which again means you got the server name wrong. '未知主机',这又意味着您的服务器名称错误。

I don't know if I'm suposed to laugh or cry now. 我不知道我现在是笑还是哭。 A friend told me that MAYBE, maybe the problem might be that I try to log in with the external IP from a network-intern PC, which for some mysterious reason can't get out or can't get back in. Got someone to try and login from somewhere else, and voila! 一位朋友告诉我,MAYBE,也许问题可能是我尝试使用来自网络实习生PC的外部IP登录,由于一些神秘的原因无法退出或无法重新进入。有人来尝试从其他地方登录,瞧! Works like a charm. 奇迹般有效。

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

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