0

im trying to talk between my java app and python client. But it cant seem to connect. I am not sure if it is because of a wrong IP-adress. Because i read somewhere that you can use the IP of the PC to connect to the virtual device, but i also used the 10.0.x.x address of the virtual device which also doesnt seem to work. In IntelliJ my code does seem to work, only not with the app.

my python client code:

import socket
HOST = "172.20.10.12"
PORT = 8080

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
print('connected')

sock.sendall(b'Hello!')
data = sock.recv(1024)
print("1", data, '\n')

sock.sendall(b'Bye\n')
data = sock.recv(1024)
print("2", data, '\n')

print("Socket closed")
sock.close()

And this is the receiving side of my app: StartReceive.java:

package com.example.test;

import java.io.IOException;

class StartReceive implements Runnable {

    @Override
    public void run() {
        try {
            Receive.startReceive();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

My receive.java:

public class Receive {

    static String fromClient;
    static String toClient;

    public static void startReceive() throws IOException {

        ServerSocket server = new ServerSocket(8080);
        System.out.println("wait for connection on port 8080");

        boolean run = true;
        while (run) {
            Socket client = server.accept();
            System.out.println("got connection on port 8080");
            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            PrintWriter out = new PrintWriter(client.getOutputStream(), true);

            fromClient = in.readLine();
            System.out.println("received: " + fromClient);

            if (fromClient != null) {
                toClient = "message is received on server";
                System.out.println("send: message is received");
                out.println(toClient);
                fromClient = in.readLine();
                System.out.println("received: " + fromClient);

                if (fromClient.equals("Bye")) {
                    toClient = "bye received on server";
                    System.out.println("send: bye received on server");
                    out.println(toClient);
                    client.close();
                    run = false;
                    System.out.println("socket closed");
                }
            }
        }
    }
}

And then i call it in my mainactivity like this:

enter image description here

4
  • Can you tell us what prints on the server side? Commented Dec 18, 2021 at 19:54
  • @markspace Hi, It prints till: wait for connection on port 8080 Commented Dec 18, 2021 at 19:55
  • If you can't accept incoming connections, it might be your firewall. Commented Dec 18, 2021 at 19:56
  • @markspace that could be indeed. Do you know how to fix that? Commented Dec 18, 2021 at 19:57

1 Answer 1

1

To debug the connection issue, first try and set the IP to "localhost" on the client.

You might also usefully print server.getInetAddress() on the server side. It's possible you're not listening on the specific address that your client wanted to connect to.


I would debug this as follows:

  1. Run the server

  2. On the server PC, type the command 'netstat -a -p tcp' (this is Windows, right) and look for something you recognize as the server. The "virtual android device" must show up on the physical PC, since the physical PC is the thing that is physically connected to the physical network.

  3. Try and connect to that server using some utility or other; I prefer telnet. 'telnet '. You should at least see your server's "connected" message on the server console output.

If this all goes well, you now know the server is basically functional.


Once your client is connected, you have a further bug:

You are sending 6 bytes, no newline.

sock.sendall(b'Hello!')

You are expecting to receive data terminated by a newline.

fromClient = in.readLine();

Your receiver is still waiting for the end of the line.

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

9 Comments

Hi there. With localhost it doesnt seem to work either. Even turned off firewall of windows. I truly do not seem to find the issue. It works fine with IntelliJ, just not with the app.
Did you print the listening address in the server? (Right after you initialize the server socket). What was the output? What OS is this, anyway?
I am running the android app (virtual device) on my windows machine. The client is running on my raspberry pi.
I got some extra info when debugging, if it helps: server: "ServerSocket[addr=0.0.0.0/0.0.0.0,localport=8080]". client: "Socket[addr=/172.20.10.10,port=47104,localport=8080]"
Your client is bound to local port 8080 and is trying to connect to port 47104 on the server? That doesn't seem right and does not match the posted code. Did you mistype that?
|

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.