Reputation: 5727
I am trying to connect to java (server) from android phone (client). Server code:
import java.net.*;
/**
* Created by mwalko on 07.06.16.
*/
public class Main {
public static void main(String[] args)
{
try
{
ServerSocket sock = new ServerSocket (8601);
for (;;)
{
Socket newsock = sock.accept();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Then I can talk with it by cURL:
curl localhost:8601
Hello :: enter QUIT to exit
From server: GET / HTTP/1.1.
From server: Host: localhost:8601.
From server: User-Agent: curl/7.47.0.
From server: Accept: */*.
From server: .
But when I try to connect from android which works on this code (I also added <uses-permission android:name="android.permission.INTERNET" />
to AndroidManifest.xml):
new Thread() {
@Override
public void run() {
try {
Socket s = new Socket("192.168.1.102", 6000);
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
I get an error:
06-07 20:09:03.530 7319-7319/com.example.root.client E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.root.client, PID: 7319
06-07 21:45:20.990 25482-25520/com.example.root.client W/System.err: java.net.ConnectException: failed to connect to /192.168.1.102 (port 6000): connect failed: ECONNREFUSED (Connection refused)
I can ping 192.168.1.101 from phone. Why can't I connect to server, are there any bugs?
Upvotes: 0
Views: 528
Reputation: 11
If you read the error message in the debug console, it states.
failed to connect to /192.168.1.102 (port 6000): connect failed: ECONNREFUSED (Connection refused)
Your Server code has a socket for port 8601, which means it is expecting requests at that port, not 6000. Change your android request port to 8601, or server listening port to 6000.
You also state that you can ping 192.168.1.101, but in the java client you are creating a connection to IP 192.168.1.102. For example, if server code was running on 192.168.1.101, then every request from the android client (192.168.1.102 in this case) should be forwarded to 192.168.1.101, through port 8601, usually written as: 192.168.1.101:8601
Upvotes: 1