Reputation: 46
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:
Upvotes: 0
Views: 127
Reputation: 1113
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:
Run the server
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.
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.
Upvotes: 1