[英]python client cant connect to java server
im trying to talk between my java app and python client.我试图在我的 java 应用程序和 python 客户端之间交谈。 But it cant seem to connect.但它似乎无法连接。 I am not sure if it is because of a wrong IP-adress.我不确定是不是因为IP地址错误。 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.xx address of the virtual device which also doesnt seem to work.因为我在某处读到您可以使用 PC 的 IP 连接到虚拟设备,但我也使用了虚拟设备的 10.0.xx 地址,这似乎也不起作用。 In IntelliJ my code does seem to work, only not with the app.在 IntelliJ 中,我的代码似乎确实有效,只是不适用于应用程序。
my python client code:我的 python 客户端代码:
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:这是我的应用程序的接收端: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:我的接收.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:然后我在我的主要活动中这样称呼它:
To debug the connection issue, first try and set the IP to "localhost" on the client.要调试连接问题,首先尝试在客户端上将 IP 设置为“localhost”。
You might also usefully print server.getInetAddress() on the server side.您还可以在服务器端打印 server.getInetAddress() 。 It's possible you're not listening on the specific address that your client wanted to connect to.您可能没有在侦听客户想要连接的特定地址。
Once connected, you have a further bug:连接后,您还有一个错误:
You are sending 6 bytes, no newline.您正在发送 6 个字节,没有换行符。
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.您的接收器仍在等待线路结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.