简体   繁体   English

Java 无法将我自己的公共 ip 分配给 SocketServer

[英]Java can't assign my own public ip to SocketServer

I am implementing a simple client server app in java.我正在 java 中实现一个简单的客户端服务器应用程序。 I want to make the server available on my public ip but when I create the soccer server I get the following:我想让服务器在我的公共 ip 上可用,但是当我创建足球服务器时,我得到以下信息:

        at java.base/sun.nio.ch.Net.bind0(Native Method)
        at java.base/sun.nio.ch.Net.bind(Net.java:555)
        at java.base/sun.nio.ch.Net.bind(Net.java:544)
        at java.base/sun.nio.ch.NioSocketImpl.bind(NioSocketImpl.java:643)
        at java.base/java.net.ServerSocket.bind(ServerSocket.java:388)
        at java.base/java.net.ServerSocket.<init>(ServerSocket.java:274)
        at Server.Server.run(Server.java:33)
        at java.base/java.lang.Thread.run(Thread.java:833)
Exception in thread "Thread-0" java.lang.NullPointerException: Cannot invoke "java.net.ServerSocket.accept()" because "this.serverSocket" is null
        at Server.Server.run(Server.java:41)
        at java.base/java.lang.Thread.run(Thread.java:833)

Below you can find the Server class that creates the server.您可以在下面找到创建服务器的服务器 class。


import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server implements Runnable {
    private int port;
    private String ip;
    private int backlog;
    private ServerSocket serverSocket;
    private boolean running=false;
    private InetAddress address;

    public Server(int port, String ip, int backlog) {
        this.port=port;
        this.ip=ip;
        this.backlog=backlog;
    }
    public void start(){
        System.out.println("Server started");
        new Thread(this).start();

    }
    @Override
    public void run() {
        running=true;
        ExecutorService executor= Executors.newCachedThreadPool();
        try{
            serverSocket=new ServerSocket(port, backlog,InetAddress.getByName(ip));
            address=serverSocket.getInetAddress();
            System.out.println("ServerSocket created at: "+address.getHostAddress()+":"+port);
        }catch (IOException e){
            e.printStackTrace();
        }
        while (running){
            try{
                Socket socket=serverSocket.accept();
                executor.submit(new EchoServerClientHandler(socket));
                System.out.println("Client connected");

            }catch (IOException e){
                e.printStackTrace();
            }
        }
        shutdown(executor);
    }

    public void shutdown(ExecutorService executor){
        running=false;
        try{
            executor.shutdown();
            serverSocket.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

When I use localhost the server works fine, but I want to make it accessible from the outside.当我使用 localhost 时,服务器工作正常,但我想让它从外部访问。 How do I fix this?我该如何解决? And also, how do I make my server accessible from the outside?还有,我如何让我的服务器可以从外部访问?

You can only listen on interfaces (=IP addresses) that are present on the machine your code is running on.您只能侦听运行代码的机器上存在的接口(=IP 地址)。

If you've got a NAT router in between you and the world, then you need (1) your code to listen on a local address, and (2) the router to forward messages from its public interface to your computer.如果您和世界之间有一个 NAT 路由器,那么您需要 (1) 您的代码侦听本地地址,以及 (2) 路由器将消息从其公共接口转发到您的计算机。 This is a router configuration issue.这是路由器配置问题。 Setting it up manually is easiest.手动设置是最简单的。

(Your error handling is deficient as well; if you fail in the socket setup, you just print a stack trace and then carry on running - carrying on after failing to get a socket is not a good idea) (您的错误处理也有缺陷;如果您在套接字设置中失败,您只需打印一个堆栈跟踪,然后继续运行 - 在获取套接字失败后继续运行不是一个好主意)

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

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