Search code examples
javasocketsexceptionnetwork-programmingartificial-intelligence

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


I must to create an IA on a game.

To do this I must connect me to a server but I can't..

I will explain. My teacher take my java project and execute Launching.class. In the file Launching.java and Connection.java I try to connect me on the server.

When my teacher executes the file, the arguments are: server_name, port_number on the server and size of a map (not important here)

I created a local server and all is good but when I send my file and when my teacher test it, he sends me that there is an error :

Serveur : accept() du serveur pour le second joueur (n° 1) impossible ; java.net.SocketTimeoutException: Accept timed out

My code is simple, I think, so I ask for help.

To connect me I use two files "Launching.jaa" and "Connection.java" below:

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();
    }   
}

So have you an idea to help me? I would like to keep this architecture..


Solution

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

    This always assigns the string "adressIP" to adress, instead of the parameter adressIP.