Java服务器套接字在不同路由器之间的连接

huangapple go评论77阅读模式
英文:

Java Server Socket connection over different routers

问题

我目前正在开发一个小游戏的客户端和服务器。
连接到服务器的客户端使用以下方法建立连接:

// 调用此方法,传递一个IPv6地址和端口号6666
public void startConnection(String ip, int port) throws IOException {
    try {
        clientSocket = new Socket(ip, port);
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        // 处理响应的一些其他代码
    } catch (IOException e) {
        LOG.debug("初始化连接时出错", e);
        throw new IOException();
    }
}

我构建的服务器使用以下方法接受连接:

public void start(int port) {
    try {
        serverSocket = new ServerSocket(port); // 端口 = 6666
        // 此部分用于同时处理多个连接
        while (b){
            try {
                map.add(new EchoClientHandler(serverSocket.accept())); // EchoClientHandler是一个用于发送和接收数据指令的类
                x = map.size() - 1;
                System.out.println("从端口建立连接:" + port);
                map.get(x).start();
                System.out.println("连接已建立");
            } catch (SocketTimeoutException e) {
                
            }
        }
    } catch (IOException e) {
        // 处理异常的代码
        e.printStackTrace();
    }
}

这两种方法都可以正常工作,并在客户端和服务器之间建立了稳定的连接,但当我尝试从不同的路由器或一般的互联网连接(如通过蜂窝数据)建立连接时,它不起作用。
是否有一种方法可以在不要求客户端和服务器必须从同一个路由器连接的情况下建立连接?

编辑:
这是客户端收到的错误,服务器不显示任何内容:

18:03:24.288 [AWT-EventQueue-0] DEBUG dorusblanken.werwolfclient.Client - 初始化连接时出错

java.net.SocketException: 网络不可达: 连接
英文:

I am currently developing a client and a server for a small game.
The client which connects to the server establishes the connection with this method:

// This method is called, passing on an ipv6 address and port number 6666
	    public void startConnection(String ip, int port) throws IOException {
	        try {
	            clientSocket = new Socket(ip, port);
	            out = new PrintWriter(clientSocket.getOutputStream(), true);
	            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                //some other code handling responses
	        } catch (IOException e) {
	            LOG.debug("Error when initializing connection", e);
	            throw new IOException();
	        }

	    }

The Server I built accepts connections using this method:

 public void start(int port) {
        try {
			serverSocket = new ServerSocket(port); //port = 6666
//This part is used to handle multiple connections at once
        while (b){
        	try {
				map.add(new EchoClientHandler(serverSocket.accept())); //EchoClientHandler is a class used to send and receive data instructions 
	        	x = map.size() - 1;
				System.out.println("Establishing connection from port " + port);
				map.get(x).start();
				System.out.println("Connection established");
        	} catch (SocketTimeoutException e) {
        		
        	}
		}
        }catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        }

Both methods work fine and establish a stable connection between the client and the server, but when i try and establish a connection from different routers or general internet connections (like via cellular data) it doesn't work.
Is there a way to establish connections without both the client and the server having to connect from the same router?

Edit:
Here is the error i get from the client, the server doesn't show anything:

18:03:24.288 [AWT-EventQueue-0] DEBUG dorusblanken.werwolfclient.Client - Error when initializing connection

java.net.SocketException: Network is unreachable: connect

答案1

得分: 1

"Network is unreachable" 意味着从当前网络无法到达目标网络。

你提到你正在尝试通过互联网建立连接。为了使其正常工作,目标主机(您的服务器)必须连接到互联网,必须具有公共IP地址,并且客户端在连接时需要使用公共IP地址。

这是最简单的配置。大多数公司实际上并不直接将他们的服务器放在互联网上。相反,公共IP通常属于CDN或DDoS防护层,它会将连接转发到负载均衡器,而负载均衡器会将连接转发到服务器。

英文:

"Network is unreachable" means there is no way to get to the destination network from the current network.

You mentioned that you are trying to establish a connection via the Internet. For that to work, the destination host (your server) must be connected to the Internet, it must have a public IP address, and the clients need to use the public IP address when connecting.

That is the simplest configuration. Most companies don't actually put their servers directly on the Internet. Instead, the public IP frequently belongs to a CDN or DDoS mitigation layer, which forwards connections to a load balancer, and the load balancer forwards connections to servers.

huangapple
  • 本文由 发表于 2020年8月10日 00:09:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63328584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定