ealeon ealeon - 2 months ago 8
Perl Question

python client and perl server: packing and unpacking bytes to send/receive

python_client.py

def send_one_message(sock, data):
length = len(data)
sock.sendall(struct.pack('!I', length))
sock.sendall(data)


perl_server.pl

sub ntohl {
unpack("I", pack("N", $_[0]));
}

my $line = "";
$client_socket->recv($line, 4);
my $line_length = ntohl($line);
print "expected to receive $line_length bytes\n";

$client_socket->recv($line, $line_length);
print "$line\n";


I get this error:

Argument "\0\0\0C" isn't numeric in pack
in perl_server.pl

I dont think I am unpacking correctly in perl_server.pl

Any suggestions?

Answer

I've changed ntohl

sub ntohl {
    unpack("I",  $_[0]);
}

and

sock.sendall(struct.pack('I', length))