如何在一台连接到不同计算机/网络的 IP 地址的计算机上创建服务器套接字? 新的编码

问题描述

我的输出:

==== Game Server ====
The port is: 8000
java.net.BindException: Cannot assign requested address: JVM_Bind
Waiting for connections...
java.net.SocketException: Socket is not bound yet

错误来自哪里:

    port = 8000;
    System.out.println("The port is: "+port);
    try{
        InetAddress ip = InetAddress.getByName("10.117.67.164");
        ss = new ServerSocket();
        SocketAddress endPoint = new InetSocketAddress(ip,port);
        ss.bind(endPoint);
    } catch(IOException ex){
        System.out.println(ex);
    }

我正在尝试使用连接到服务器的两个客户端来创建游戏。如果我将服务器绑定到“localhost”或我计算机的 IP,一切正常,但是当我尝试不同的 IP 时,会出现此异常。如果没有解决办法也没关系,我可以随时让客户端询问游戏服务器绑定的IP。

此代码有效:

    port = 8000;
    System.out.println("The port is: "+port);
    try{
        InetAddress ip = InetAddress.getByName("localhost");
        ss = new ServerSocket();
        SocketAddress endPoint = new InetSocketAddress(ip,port);
        ss.bind(endPoint);
    } catch(IOException ex){
        System.out.println(ex);
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)