I'm making a python server running on my computer and java socket client running on my phone. the minute the java tries to connect, the app freezes. Not saying anything in the logcat. I have no idea why...
python 3.5.1:
PORT = 8888
import socket
sock = socket.socket()
sock.bind(("0.0.0.0", PORT))
sock.listen(1)
client = None
while not client:
try:
client = sock.accept()
except:
pass
print(client[1])
java:
static String get_leaderboard() {
Socket sock;
OutputStream out;
InputStream in;
try {
System.out.println("now trying to connect");
sock = new Socket("192.168.1.29", 8888);
System.out.println("connected successfuly");
out = sock.getOutputStream();
in = sock.getInputStream();
return Integer.toString(in.read());
} catch (IOException e) {
e.printStackTrace();
}
return "Error in connection";
but actually, logcat never prints "connected successfuly". just "trying to connect".
This line is wrong:
client = sock.connect()
Servers don't call connect()
. Servers call listen()
and accept()
. Only clients call connect()
.
Since your server calls bind()
and listen()
, but not accept()
, any client will hang trying to connect to it.
Resources:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.