Send an integer value through sockets in perl.

T

Tompyna

Hi All,
I am very much new to socket programming in perl. I had written
a small socket client and server program to send and receive a string
value.
my $msg = "hello";
send(SOCKET,$msg,10,MSG_OOB ) ----- > will send the string "hello".

With the above code i was able to send an string value, and receive
the value in the client.Now my starts here.... .

But if i need to send an integer. i tried sending by giving

my $msg = 10;

$Returned_val = send(SOCKET,$msg,4,MSG_OOB );

print "\nNo: of characters send '",$Returnned_val,"'";

This will take 10 as string value and send it.
Because the printed Returned_val == 2 , and not 4 .

Can anyone suggest any way to send an integer value through sockets in
perl?.

Thanx.... in advance.
Thomas Poly.
 
T

Tassilo v. Parseval

Also sprach Tompyna:
I am very much new to socket programming in perl. I had written
a small socket client and server program to send and receive a string
value.
my $msg = "hello";
send(SOCKET,$msg,10,MSG_OOB ) ----- > will send the string "hello".

With the above code i was able to send an string value, and receive
the value in the client.Now my starts here.... .

But if i need to send an integer. i tried sending by giving

my $msg = 10;

$Returned_val = send(SOCKET,$msg,4,MSG_OOB );

print "\nNo: of characters send '",$Returnned_val,"'";

This will take 10 as string value and send it.
Because the printed Returned_val == 2 , and not 4 .

So you don't actually want to send the string "10" but rather 10 as a
32bit integer? In this case you have to pack it. You can choose between
two byteorders. Network byteorder (bigendian) seems appropriate here:

my $msg = pack "N", 10;
my $ret = send(SOCKET, $msg, 4, MSG_OOB);

The receiver then just has to unpack it again:

my $num = unpack "N", $received;

Tassilo
 
B

Ben Morrow

Hi All,
I am very much new to socket programming in perl. I had written
a small socket client and server program to send and receive a string
value.
my $msg = "hello";
send(SOCKET,$msg,10,MSG_OOB ) ----- > will send the string "hello".

Are you sure you want to send the value as out-of-band data? Do you not
just want to send it normally:

my $msg = pack 'a10', 'hello'; # null-padded, use A10 for space-padded
print SOCKET $msg;

Also, you may find life easier if you use the IO::Socket modules.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top