2

I want to connect via socket to my android app.but in server side(android app) I get java.net.SocketTimeoutException error and in client side I get java.net.ConnectException: Connection refused: connecterror. what is my mistake? thank you

server (android app)

public class ServerSocketTask extends AsyncTask<Void, Void, String> {
final StackTraceElement se = Thread.currentThread().getStackTrace()[2];

private String data = null;

@Override
protected String doInBackground(Void... params) {
    Log.d(se.getClassName() + "." + se.getMethodName(), "start");
    try {
        ServerSocket serverSocket = new ServerSocket(8989);
        serverSocket.setSoTimeout(50000);
        Socket socket = serverSocket.accept();
        socket.setKeepAlive(true);

        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        int readed = in.read();
        Log.d("","readed bytes : "+readed);

        String line;
        while ((line = in.readLine()) != null){
            Log.i("","line : "+ line);
        }
        socket.close();
        serverSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    ServerSocketTask.this.data = result;
}

public String getData() {
    return data;
}

}

client

    public static void main(String[] args) {
    int port;
    try (Socket socket = new Socket("192.168.240.105", 8989)) {
        String customerId = "123";
        String requestId = Configuration.getProperty("requestId");
        ClientService result = new ClientService();
        String makeRequest = result.objectToJson(customerId, requestId);
        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        writer.write(makeRequest);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

my client can't connect to server and my server wait for connection.

5
  • Can you verify that you can connect to socket through telnet 192.168.240.105 8989? Commented Jan 10, 2016 at 8:28
  • Could not open connection to the host, on port 8989: Connect failed. 192.168.240.105 is my machine ip, even with localhost and 127.0.0.1 I get same error for telnet Commented Jan 10, 2016 at 8:32
  • why my server doesn't listen to 8989? Commented Jan 10, 2016 at 8:48
  • Using emulator or device? Commented Jan 10, 2016 at 9:23
  • '192.168.240.105 is my machine ip, '. What do you mean with 'machine'? The client should use the ip of your Android device. You cannot use an emulator in your setup. Commented Jan 10, 2016 at 9:30

1 Answer 1

2

When you construct ServerSocket(8989) you're binding to wildcard address of network interfaces available on android emulator/device.

However both Android emulator and real device has it's own network interface(s) and thus it's it's own IP addresses. Your client program (development machine) IP address is not the same as IP address of android emulator/device. In other words you cannot connect to the socket created in Android app because you're using wrong address.

This answer should guide you on how to find out the address.

Sign up to request clarification or add additional context in comments.

3 Comments

He isn't using the wrong address. A listening socket bound to the wildcard address will accept connections from any of the local IP addresses,
Yes, but he's trying to use his development machine ip address and android device/emulator will have different local addresses.
You mean 'the client uses the ip of the machine where it is running on'. Or it's own ip. Your talking about wildcard does not make sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.