Java套接字异常:Java.net.SocketTimeoutException:接受超时

Java套接字异常:Java.net.SocketTimeoutException:接受超时,java,sockets,exception,network-programming,artificial-intelligence,Java,Sockets,Exception,Network Programming,Artificial Intelligence,我必须在游戏中创建IA 为此,我必须将我连接到服务器,但我无法 我会解释的。我的老师拿着我的java项目执行Launching.class。 在文件Launching.java和Connection.java中,我尝试在服务器上连接我 当我的老师执行文件时,参数是:服务器名称、服务器上的端口号和映射的大小(这里不重要) 我创建了一个本地服务器,一切正常,但当我发送文件时,当我的老师测试文件时,他向我发送错误信息: 服务员:不可能接受第二次(第1次)的服务员;java.net.SocketTime

我必须在游戏中创建IA

为此,我必须将我连接到服务器,但我无法

我会解释的。我的老师拿着我的java项目执行Launching.class。 在文件Launching.java和Connection.java中,我尝试在服务器上连接我

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

我创建了一个本地服务器,一切正常,但当我发送文件时,当我的老师测试文件时,他向我发送错误信息:

服务员:不可能接受第二次(第1次)的服务员;java.net.SocketTimeoutException:接受超时

我认为我的代码很简单,所以我请求帮助

要连接我,我使用下面的两个文件“Launching.jaa”和“Connection.java”:

启动.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();
        }
    }
}
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();
    }   
}
Connection.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();
        }
    }
}
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


这总是将字符串
“adressIP”
分配给adress,而不是参数
adressIP

如果服务器获得
accept()
超时,则只能表示连接失败,这意味着IP地址或端口错误

然而,还有许多其他问题

Connection connection = new Connection(parametersLaunching.addressIP, parametersLaunching.port);
这里,您(大概)正在创建一个连接到目标的客户机
Socket

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();
在这里,您将再次关闭
套接字。移除

 Game game = new Game(connection.getInputStream(), connection.getOutputStream(), Integer.parseInt(parameters[2]));

注意:没有必要关闭所有这三项:只需输出流就足够了。关闭其中一个流将关闭另一个流和
套接字

如果服务器获得
accept()
超时,则只能表示连接失败,这意味着IP地址或端口错误

然而,还有许多其他问题

Connection connection = new Connection(parametersLaunching.addressIP, parametersLaunching.port);
这里,您(大概)正在创建一个连接到目标的客户机
Socket

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();
在这里,您将再次关闭
套接字。移除

 Game game = new Game(connection.getInputStream(), connection.getOutputStream(), Integer.parseInt(parameters[2]));

注意:没有必要关闭所有这三项:只需输出流就足够了。关闭其中一个流将关闭另一个流和
套接字

“我创建了一个本地服务器”,但您尚未发布它。因为它非常糟糕。。伟大的服务器是我老师的服务器,但我们没有代码。我想我发现了我的错误。。在Connection.java中:InetAddress address=InetAddress.getByName(“adressIP”);我认为它是:InetAddress address=InetAddress.getByName(adressIP);我会再测试一次,如果一切顺利,我会告诉你;)“我创建了一个本地服务器”,但你还没有发布。因为它非常糟糕。。伟大的服务器是我老师的服务器,但我们没有代码。我想我发现了我的错误。。在Connection.java中:InetAddress address=InetAddress.getByName(“adressIP”);我认为它是:InetAddress address=InetAddress.getByName(adressIP);我会再测试一次,如果一切顺利,我会告诉你;)非常感谢你!我很笨。。我会再测试一次,然后告诉你一切是否都好!:D但再次感谢!:)非常感谢你!我很笨。。我会再测试一次,然后告诉你一切是否都好!:D但再次感谢!:)非常感谢您的帮助!:)我会检查我的代码啊哈!非常感谢您的帮助!:)我会检查我的代码啊哈!