I have a client in Python that sends data (preceded by a data length message):
s = socket.socket()
s.connect((host, port))
data = 'hello world'
s.sendall('%16s' % len(data)) #send data length
s.sendall(data) #send data
s.close()
And a server in Java that receives the data. The server uses DataInputStream.readInt() to read the data length before reading the data. However I seem to be getting really large numbers returned by readInt(). What is the problem?
Java expects the binary representation of your integer. You can use the struct module to generate binary representations.
In your case, this would be:
Also make sure you use the correct byte order. Java could be expecting network byte order.