Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
There's more than one way to do things
 
PerlMonks  

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 "flashpolicyd.pl" 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 "flashpolicyd.pl" script: (*I can see my PC's IP Address)

   "Connection from 192.168.x.xxx:56321
    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 "flashpolicyd.pl"...

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('192.168.5.26', 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 search.cpan.org:
  "http://search.cpan.org/~gaas/LWP-attic-1.00/lib/LWP/Socket.pm"


If you need to see the "flashpolicyd.pl" 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,
Matt


Comment on Error while using LWP::Socket to send XML Data
Download Code
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 --> "http://search.cpan.org/~dapm/perl-5.14.4/ext/Socket/Socket.pm"?

      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!


      Thanks,
      Matt

        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 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

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 # client.pl #---------------- 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 = "192.168.x.xxx"; 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:

       DOCUMENTATION:
          http://search.cpan.org/~dapm/perl-5.14.4/ext/Socket/Socket.pm
       REALLY GOOD EXAMPLES HERE:
          http://www.binarytides.com/perl-socket-programming-tutorial/
          http://www.herongyang.com/Perl/Socket-connect-Establish-Socket-Communication.html


    Thanks Again,
    Matt

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-04-20 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (487 votes), past polls