1
votes

I have a problem when I try to connect my physical device to my server using sockets. On the server side it does not seem to accept any connection while on the client side the socket times out. Any ideas why this is happening?

I provide my code below

Server Code:

public void run()
    {
        // TODO Auto-generated method stub
        try{
        gamePending = false;
        pid = 0;
        while(pid < 2){
            System.out.println("Hello from run loop on game");
            Socket tempSocket = server.accept();
            System.out.println("Client connected at " + tempSocket.getLocalPort());
            PrintWriter tempWriter = new PrintWriter(new BufferedWriter (new OutputStreamWriter(tempSocket.getOutputStream())),true);
            tempWriter.println("" + pid);

            players[pid] = new Client(tempSocket, pid, this);
            players[pid].start();
            gamePending = true;
            if(pid == 0){sendMsg(pid, "waiting for other player");}
            pid++;
        }
        }
        catch(Exception e){
            System.out.println("There has been an Error. Game will be Terminated.");
        }
        //Start new game for the next two players...
        new Game();
    }

Client Side:

public void run() {
    // Connects to the Server....
    try {
        socket = new Socket("192.168.1.116", 9090);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        in = new BufferedReader (new InputStreamReader(socket.getInputStream()));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        out = new PrintWriter(socket.getOutputStream(),true);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

11-16 23:32:11.016: W/System.err(24213): java.net.ConnectException: failed to connect to /192.168.1.116 (port 9090): connect failed: ETIMEDOUT (Connection timed out) 11-16 23:32:11.016: W/System.err(24213): at libcore.io.IoBridge.connect(IoBridge.java:114) 11-16 23:32:11.016: W/System.err(24213): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 11-16 23:32:11.026: W/System.err(24213): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 11-16 23:32:11.026: W/System.err(24213): at java.net.Socket.connect(Socket.java:842) 11-16 23:32:11.026: W/System.err(24213): at vatos.locos.spheroknockout.Connection.run(Connection.java:22) 11-16 23:32:11.026: W/System.err(24213): at java.lang.Thread.run(Thread.java:841) 11-16 23:32:11.026: W/System.err(24213): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out) }

1
Try telnetting from client box to your server IP/port. Does it connect?Martin James

1 Answers

2
votes

I can't be sure (because it does not appear in your code) but I think the server is not hosing on the same port (9090). That may be the main problem, but the server or client may also be blocked by a firewall (even if they run on the same machine).