I made a python server and a java client. My problem is simple: The server receives the message from client, but the client doesn't get the reply.
Java Client:
package fgd;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
public class fdassd {
public static void main(String[] args){
new Thread(){
public void run(){
while (true)
{
try {Socket socke=new Socket("censored",1977);
DataOutputStream dout=new DataOutputStream(socke.getOutputStream());
DataInputStream din = new DataInputStream(socke.getInputStream());
dout.writeUTF("Heey");
dout.flush();
String str = din.readUTF();
System.out.println(str);
dout.close();
socke.close();
}catch(Exception e){
e.printStackTrace();
}
try {
Thread.sleep(17000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
}
Python Server:
hosto = '0.0.0.0'
porto = 1979
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created!'
try:
soc.bind((hosto, porto))
except socket.error as e:
print(e)
sys.exit()
print 'Socket bind complete'
soc.settimeout(30)
soc.listen(10)
print 'Listening...'
timeout = 8
timeout_start = time.time()
while time.time() < timeout_start + timeout:
try:
conn, addr = soc.accept()
if addr[0] != opip:
conn.shutdown(socket.SHUT_RDWR)
conn.close()
else:
msg = conn.recv(1024)
print ('--------------------------------------')
print (msg)
conn.send((playername).encode('UTF-8'))
print ('Success! The following command has been sent to: ' + opip + ':' + playername )
print ('--------------------------------------')
soc.close()
break
except socket.timeout as e:
print(e,': Server not online or wrong ip')
soc.close()
break
else:
I've seen a very similar question where the answer was to add to lines before conn.send (Link: Socket Java client - Python Server). But I can't use the solution in that question, because
conn.send(len(message_to_send).to_bytes(2, byteorder='big'))
doesn't seem to work in python 2.x .That means I need another solution to send the message with UTF-8 but I can't figure out what to do.
Regards