0

I have server on my laptop and an Android application that connects to it.

When i emulate the app on the same computer as the server i can connect to it. But when i put the application on my phone it throws this error:

java.net.connectexception: failed to connect to /*** (port 4444): connect failed: Etimedout (Connection timed out)

I have tried different ip:s and forwarded port, but it does not make any difference.

Server Code:

import java.net.ServerSocket;
import java.net.Socket;

public class Server
{
    static Socket csocket;
    Socket sock;

    Server(Socket csocket, Socket sock) 
    {
        this.csocket = csocket;
        this.sock = sock;
    }

    public static void main(String[] args) throws Exception
    {
        ServerSocket ServerSock = new ServerSocket(4444);
        System.out.println("Listening...");

        while (true) 
        {
            Socket sock = ServerSock.accept();
            System.out.println("Connected");

            System.out.println("Starting Client " +  sock.getPort() );
            new Thread (new ClientThread(sock)).start();    
        }
    }   
}

Client Code (function connect):

public static String connect()
{
    try
    {
        MySocket = new Socket("**.***.***.**", 4444); //MySocket is a static socket
        return receive().get("protocol").toString();//'ok' signal from server
    } catch (Exception e) {
        return e.toString();
    }
}
nios
  • 130
  • 12
  • Does the server code succesfully started up on the phone? If so, could there be a security restriction on how Java running on the phone? – Minh Kieu May 29 '17 at 17:57
  • The server code run on the leptop, not on the phone. On the phone there is only the apk with the function i quated – Noam Givaty May 29 '17 at 18:48
  • Did you check your firewall settings, i.e. do they allow incoming connections on your laptop to that port? See also https://stackoverflow.com/a/23854174/233645 – Kriegel May 29 '17 at 21:05
  • i add 2 allow connection on TCP and UDP on 4444 in inbound rules at the Windoes firewall, and it still not working... @Kriegel – Noam Givaty May 30 '17 at 09:14
  • Ok, i managed to connect succecfuly after switch off my firewall. how can i make a better solution for that? i already add allowed ports – Noam Givaty May 30 '17 at 13:02
  • after alot of searching i got this solution to work: https://stackoverflow.com/a/35713399/8044695 – Noam Givaty May 31 '17 at 07:34

0 Answers0