ealeon ealeon - 1 year ago 81
Perl Question

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


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


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 Source

I've changed ntohl

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


sock.sendall(struct.pack('I', length))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download