I have been looking for so long for a way to connect to a server created in Python using java.
Can anyone show me how to connect with java and how to send string? It is recommended that it also works on Android
My server in python:
import socket, time
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.bind(("160.07.08.49", 6784))
soc.listen(5)
(client, (ipNum, portNum)) = soc.accept()
while True:
print(client.recv(1024))
time.sleep(0.5)
My client in Java:
try {
Socket socket = new Socket("160.07.08.49", 6784);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
printWriter.write("Hello from java");
printWriter.flush();
printWriter.close();
}catch (Exception e) {e.printStackTrace();}
And I got an error from python when the Java client connected
print(soc.recv(20))
A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied