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