Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl byte array send through socket

Status
Not open for further replies.

raileros

Programmer
Joined
Feb 9, 2004
Messages
2
Location
PL
Hello everyone!

I have such a code in my Java application that I would like to convert to Perl. The problem is I don't know how to send a byte array through a socket in Perl.

The Java code looks like this:

byte[] request = new byte[4];

request[0] = 17;
request[1] = 0;
request[2] = 0;
request[3] = 0;

Socket socket = new Socket(hostname, port);
os = new DataOutputStream(socket.getOutputStream());
os.write(request);

Generally speaking my Perl code works, it can connect to the server socket and transmit data but cannot send in a proper format as the Java application does.

Here are the core segments:

$socket = IO::Socket::INET->new(PeerPort=>$port, Proto=>'tcp', PeerAddr=>$host) or die $!;
$request = pack("i", 17, 0, 0, 0);
$socket->send($request);

I just cannot figure out how to pass the array in binary format so it would be compatible with my protocol.

Any ideas or directions on how to solve this problem are VERY welcome.

Regards,
Railer

 
Well I've not any experience with this sort of thing but I can tell you straight away that your call to pack is bnot quite right. You're asking pack to return an unsigned integer which is not what you want.

You need to look more carefully at the perlfunc manpage for details on how to build the template you want.
 
$request = pack("UUUU", 17, 0, 0, 0); did the job :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top