Java Socket Exception : java.net.SocketTimeoutException: Accept timed out

我必须在一个游戏中创建一个AI。

为此,我必须连接到一个服务器,但无法连接..

我来解释一下。我的老师拿走了我的Java项目并执行了Launching.class。在Launching.java和Connection.java文件中,我尝试连接到服务器。

当我的老师执行文件时,参数是:服务器名称、服务器端口号和地图大小(这里不重要)

我创建了一个本地服务器,一切正常,但当我发送文件并且我的老师测试时,他告诉我有一个错误:

服务器:无法为第二个玩家(编号1)进行服务器的accept()操作;java.net.SocketTimeoutException: Accept timed out

我的代码很简单,我想,所以我请求帮助。

为了连接,我使用了两个文件“Launching.java”和“Connection.java”,如下所示:

Launching.java

public class Launching{    private String addressIP;    private int port;    public Launching()    {        this.addressIP = "";        this.port = 0;    }    public void setAddressIP(String addressIP)    {        this.addressIP = addressIP;    }    public String getAddressIP()    {        return this.addressIP;    }    public void setPort(int port)    {        this.port = port;    }    public int getPort()    {        return this.port;    }    public static void main(String[] parameters) throws Exception    {        Launching parametersLaunching = new Launching();        parametersLaunching.addressIP = parameters[0];        parametersLaunching.port = Integer.parseInt(parameters[1]);        try        {            Connection connection = new Connection(parametersLaunching.addressIP, parametersLaunching.port);            connection.setInputStream(connection.getSocket());            connection.setOutputStream(connection.getSocket());            if(connection.getInputStream() != null && connection.getOutputStream() != null)            {                Game game = new Game(connection.getInputStream(), connection.getOutputStream(), Integer.parseInt(parameters[2]));                game.start();            }            if(connection.getInputStream() != null)            {                connection.getInputStream().close();            }            if(connection.getOutputStream() != null)            {                 connection.getOutputStream().close();            }            if(connection.getSocket() != null)            {                connection.getSocket().close();            }            connection.getSocket().close();        }        catch(UnknownHostException exception)        {            exception.printStackTrace();        }        catch(IOException exception)        {            exception.printStackTrace();        }    }}

Connection.java

package network;import java.io.*;import java.net.*;public class Connection{       private Socket socket;    private InputStream inputStream;    private OutputStream outputStream;    public Connection(String adressIP, int port) throws Exception    {        InetAddress adress = InetAddress.getByName("adressIP");        this.socket = new Socket(adress, port);        this.inputStream = null;        this.outputStream = null;    }    public InputStream getInputStream()    {        return this.inputStream;    }    public OutputStream getOutputStream()    {        return this.outputStream;    }    public Socket getSocket()    {        return this.socket;    }    public void setInputStream(Socket socket) throws IOException    {        this.inputStream = socket.getInputStream();    }    public void setOutputStream(Socket socket) throws IOException    {        this.outputStream = socket.getOutputStream();    }   }

那么你有帮助我的想法吗?我希望保留这个架构..


回答:

InetAddress adress = InetAddress.getByName("adressIP");

这总是将字符串 "adressIP" 赋值给 adress,而不是参数 adressIP

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注