Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Error while using LWP::Socket to send XML Data

by mmartin (Monk)
on May 09, 2013 at 15:46 UTC ( #1032785=perlquestion: print w/ replies, xml ) Need Help??
mmartin has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

So here's the story... I have a Flash program running on a server that sits and listens on a port. In order to do this
you need to have a "Flash Policy File" which I have and does work when sending XML Data from a C# program another employee
wrote. So I run the "" file and pass it my XML "Policy File" which is currently set to receive requests from
ANY host. So that part at least I know works.

So now I'm trying to send some other XML Data to the server using a Perl Script. From the Perl side of things I don't get any
errors. But when I look over at the Server that is listening for a Socket connection, I can see the following in the Windows
CMD Prompt for the output from the "" script: (*I can see my PC's IP Address)

   "Connection from
    Unrecognized request: <FRAME><MESSAGE VAR="TestVariable" TEXT="ValueOFVariable" /></FRAME>"

So for some reason the XML data I'm sending is invalid according to the Perl script ""...

My Perl script which sends the data to the server from my PC:
#!/usr/bin/perl use strict; use LWP::Socket; print "In Perl Script... Sending message now\n"; my $socket = new LWP::Socket; $socket->connect('', 843); # echo my $quote = "<FRAME><MESSAGE VAR=\"TestVariable\" TEXT=\"ValueOFVa +irable\"/></FRAME>\0"; $socket->write("$quote\n"); $socket = undef; # close

I got the code from this link below at

If you need to see the "" file too just let me know, I just got that Perl script from the Web somewhere,
and it does work for another program written in C#, which sends XML Data in pretty much the exact same format.

Anyone have any clue what I could be doing wrong here..? Is there just something I'm missing?
Any thoughts/suggestions would be VERY much appreciated.

Thanks in Advance,

Comment on Error while using LWP::Socket to send XML Data
Download Code
Replies are listed 'Best First'.
Re: Error while using LWP::Socket to send XML Data
by tobyink (Abbot) on May 09, 2013 at 15:56 UTC

    Have you read the documentation for LWP::Socket? Did you read this part...?

    Beware: New code should not use this module. The IO::Socket::INET module provide the standard Perl interface to OO Internet sockets. Even LWP is now rewritten to use IO::Socket::INET throughout.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Hey tobyink, thanks for the reply.

      No, I did not see that... But I do now, thanks.
      So, should I just use the "use Socket;" Perl Module? This one here --> ""?

      Sorry, I had just Googled something like "Perl Script, make socket connection" or something like that, and I think
      that was just one of the first ones that came up.

      Thanks again for the reply!


        The module that LWP::Socket suggests is IO::Socket::INET. That's a core module, bundled with Perl, so you already have it installed.

        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Error while using LWP::Socket to send XML Data
by mmartin (Monk) on May 10, 2013 at 16:56 UTC
    Hey Guys,

    Ok so I got this working...

    So I use this Perl Script along with another one that runs in the background on a remote terminal server.
    This script will now establish a socket connection with the remote server, send an initial "acceptance" message
    so the remote server knows it is a valid connection. If the remote server does not receive the valid string it
    will close the connection.

    Now I guess I'm at a good point where I can now use this as a base program and build off of it...
    Thanks again for your help guys... Guess it woulda been bad if I used the dead "LWP::Socket" module. So thanks for
    letting me know!

    Here's my Code:
    #!/usr/bin/perl -w # #---------------- use strict; use Socket; ### Set the Protocol we will use to connect to the Remote Server: my $proto = getprotobyname('tcp'); ### Declare my Socket Connection: my($sock); socket($sock, AF_INET, SOCK_STREAM, $proto) or die "$!"; ### Set the Remote Host's IP Address and which port we will connect to +: my $remote = ""; my $port = 3009; ### Resolve the remote IPAddr to a actual IP Address: # *'inet_aton' Takes a string representing an IP Address and conve +rts it to an ACTUAL IP Address (*not a string)... my $iaddr = inet_aton($remote) or die "Unable to resolve hostname: $re +mote... $!\n"; ### Build the Socket Address structure using PORT and IPADDR: my $peerAddress = sockaddr_in($port, $iaddr); # Initiate the Socket Connection or Exit if Failed to Connect: connect($sock, $peerAddress) or die "Connection Failed: $!\n"; print "Connected to $remote Server on port $port\n"; # Send Initial "Accept" String: # *This string MUST be sent first, because the server receiving the + data must see # this 1st in order to accept the incoming data from this script.. +. send($sock, "<getTickerData/>", 0); ### While $line is still equal to the Socket Connection Handle, then.. +. while (my $line = <$sock>) { if ($line =~ "VALID REQUEST RECEIVED") { print "***GOT A VALID RESPONSE***\n"; send($sock, "Send some data for remote server", 0); } } close($sock); exit 0;

    Thanks again for the thoughts and suggestions, very much appreciated!

    If anyone finds themselves trying to do something similar to this I found a few really good sites that helped me through this:



    Thanks Again,

Re: Error while using LWP::Socket to send XML Data
by Anonymous Monk on May 09, 2013 at 16:02 UTC

    ... LWP-attic-1.00 ...

    You found the furless bony body of a dead squirrel!:P

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1032785]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2015-11-30 08:05 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (765 votes), past polls