1

我很抱歉,但我不会说英语非常好.. :)Java Socket技术异常:java.net.SocketTimeoutException:接受超时

我一定要在游戏创建一个IA,我必须给它在5小时给我的老师。

要做到这一点,我必须我连接到服务器,但我不能..

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

当我theacher执行文件,参数为:服务器名称,端口号的地图(在这里并不重要)

我创建了一个本地服务器,一切都很好,但是当我把我的文件时的服务器和尺寸上我的老师测试它,他给我发现有一个错误:

Serveur:accept()du serveur pour le second joueur(n°1)impossible; java.net.SocketTimeoutException:接受超时

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

要连接我,我用两个文件 “Launching.jaa” 和 “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(); 
    } 
} 

那么你有想法来帮助我吗?我想保持这种架构..

坦克你非常需要你的帮助! :)

+0

“我创建了本地服务器”,但您还没有发布它。 – EJP

+0

因为它很糟糕..伟大的服务器是我老师的服务器,但我们不能拥有代码。 我想我找到了我的错误.. 在Connection.java中:InetAddress adress = InetAddress.getByName(“adressIP”); 我认为它是:InetAddress地址= InetAddress.getByName(adressIP); 我会再次测试,我会说如果一切都很好;) –

回答

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

这始终将字符串"adressIP"到ADRESS代替参数adressIP

+0

非常感谢!我很蠢.. 我会再次测试,并说如果一切都很好! :D 但是再次感谢! :) –

0

如果服务器得到一个accept()超时,它只能意味着你连接失败,这意味着一个错误的IP地址或端口。

但还有很多其他问题。

Connection connection = new Connection(parametersLaunching.addressIP, parametersLaunching.port); 

在这里,您可能(大概)创建一个连接到目标的客户端Socket

connection.setInputStream(connection.getSocket()); 
connection.setOutputStream(connection.getSocket()); 

所有这些都应该发生在Connection的构造函数中。

if (connection.getInputStream() != null && connection.getOutputStream() != null) 

这些测试是毫无意义的。他们都不可能是假的。套接字总是有输入和输出流,否则会抛出异常,大概你的Connection类不会丢掉它们。不要编写冗余代码。去掉。

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

这里你(大概)周围产生Connection.

game.start(); 

这里一个Thread要启动线程。

if(connection.getInputStream() != null) 
{ 
    connection.getInputStream().close(); 
} 
if(connection.getOutputStream() != null) 
{ 
    connection.getOutputStream().close(); 
} 
if(connection.getSocket() != null) 
{ 
    connection.getSocket().close(); 
} 

这里你防止Game从不断运行,因为你是关闭其Socket和它的两个流。去掉。当它完成后,它将自动关闭自己的套接字。去掉。

connection.getSocket().close(); 

在这里,您要关闭Socket第二次。去掉。

注意:没有必要关闭所有这三个项目:只要输出流就足够了。关闭其中一个流会关闭另一个流并关闭Socket

+0

非常感谢您的帮助! :) 我会检查我的代码啊! –