I have recently started to expand my Perl horizons into network/sockets programming, with the assistance of Dr. Stein's Network Programming with Perl. I'm shocked and embarrassed about how I've managed to get by with only a very fundamental understanding of networking protocols and TCP/IP for all these years, and now I'm faced with a stumper...

I am trying to write a script that will help me examine the contents of a mainframe database via an interface with a mysterious, black-box server that is off-site. No one seems to know anything about this server and what, exactly, it is expecting as far as message formats.

What I do have is a bunch of Java code that is behaving nicely. I hope to interpolate my own script from these heaps of Java, but I am having problems right from the beginning. The part of the Java code that is perplexing me looks like this:

OutputStream stream = s.getOutputStream(); if (stream != null) { DataOutputStream output = new DataOutputStream(stream); if (output != null) { output.writeShort(0); output.writeShort(0); output.writeShort((short) theData.length()); output.writeBytes(theData); output.flush(); res = true; } }

In this code, s is a java Socket instance that is part of the class that wraps the Socket. Another method reads from the sockets InputStream/DataInputStream. theData is a formatted string/messsage.

I understand the streams (or at least I think I do), but what's the story with the writeShort and writeBytes methods, which I am trying to emulate w/ Perl? I see from the Java docs that writeShort "Writes a short to the underlying output stream as two bytes, high byte first." and that writeBytes "Writes out the string to the underlying output stream as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits." Errrmmm... okay.

My own attempt at doing something similar looks like the following:

use strict; use warnings; use IO::Socket; my $host = '198.81.233.12'; #not real IP address my $port = 1302; #not real port my $data = '003 0000000073223 3000 +0073223091706407 20040513023'; my $msg_out = pack("nnna*", 0, 0, length $data, $data); print "\$msg_out = -$msg_out-\n"; my $socket = IO::Socket::INET->new( PeerAddr => $host, PeerPort => $port ) or die "Can't connect: $!\n"; print $socket $msg_out; my $msg_in = <$socket>; print "->$msg_in\n"; $socket->close or warn $@;

This code isn't even getting response from the socket... it never gets to the statement print "->$msg_in\n". I'm sure that I have the correct IP address and port. I chose the pack format 'nnna*' because I thought it most closely matched what the java methods were doing, but then again what do I know? I see from the perldocs that n handles a short in big-endian order, and that a handles a null-padded string. I've tried other formats, too, w/o success.

The string in $data is an exact copy of a string that I know will return a response in the java code. I should receive a long string which I can unpack or substr into individual elements.

Can anyone tell me where I am going wrong? Is it the pack format? Should I even be trying to pack the data? Am I amazingly clueless?

Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"

In reply to Converting Java Sockets to Perl Sockets by Art_XIV

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.