1

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

Louis Bernard
  • 229
  • 4
  • 20

1 Answers1

1

Add the following line, before you send the playername. This should work in both python2 and 3:

conn.send(struct.pack(">H", len(playername)))

This line will prefix the length of the player name to the message, as is required by the java DataInputStream#readUTF method. It's encoded as a two-byte integer, hence the 'H' in the struct.pack call.

You will also need an import struct statement.

See Python 2,3 Convert Integer to "bytes" Cleanly for more information.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
schtever
  • 3,210
  • 16
  • 25