Omega142
Omega142

Reputation: 313

Sending an int from a python socket to a java socket

I'm currently working on getting a python socket to send an integer to a java socket, so the java socket can read in a byte array that represents a string after this step.

I've tried sending the int in a struct but that returns a massive number on the java side.

p = struct.pack('i', len(data))
clientsocket.send(p)

Upvotes: 4

Views: 847

Answers (1)

crchurchey
crchurchey

Reputation: 188

https://docs.python.org/2/library/struct.html

Check out 7.3.2.1. When sending any data over the network always convert it to network byte-order. Your above code would be:

p = struct.pack('!i', len(data))
clientsocket.send(p)

Upvotes: 2

Related Questions