http://www.perlmonks.org?node_id=1032982

Rad has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i wrote this basic perl script to listen for connections, get the data sent on the socket. the problem i have is, I only get some of that data. here is my code-

use IO::Socket; my $sock = new IO::Socket::INET ( LocalHost => '168.159.250.206', LocalPort => '5988', Proto => 'tcp', Listen => 500, Reuse => 1, ); $sock->sockopt(SO_RCVBUF, 1500); die "Could not create socket: $!\n" unless $sock; my $new_sock = $sock->accept(); my $a = ""; while(<$new_sock>) { $new_sock->autoflush(1); print $_; $a = $a.$_; } print "a is $a"; #close($sock);

I took a network trace of my application client and server and i see that on the same port, client sends half the data and then sends the next half of data. what i get is in the below second half of data, i only get "<?xml version="1.0" encoding="UTF-8"?>"

<?xml version="1.0" encoding="UTF-8"?>.<CIM CIMVERSION="2.0" DTDVERSION="2.0"><MESSAGE ID="155178" PROTOCOLVERSION="1.0"><SIMPLEREQ><IMETHODCALL NAME="EnumerateInstances"><LOCALNAMESPACEPATH><NAMESPACE NAME="root"/><NAMESPACE NAME="brocade1"/></LOCALNAMESPACEPATH><IPARAMVALUE NAME="ClassName"><CLASSNAME NAME="Brocade_Fabric"/></IPARAMVALUE><IPARAMVALUE NAME="LocalOnly"><VALUE>true</VALUE></IPARAMVALUE><IPARAMVALUE NAME="DeepInheritance"><VALUE>false</VALUE></IPARAMVALUE><IPARAMVALUE NAME="IncludeQualifiers"><VALUE>false</VALUE></IPARAMVALUE><IPARAMVALUE NAME="IncludeClassOrigin"><VALUE>true</VALUE></IPARAMVALUE></IMETHODCALL></SIMPLEREQ></MESSAGE></CIM>

Replies are listed 'Best First'.
Re: My server doesnt get all the data sent by the client application
by vsespb (Chaplain) on May 10, 2013 at 17:23 UTC
    Works fine for me. When script ran on Linux, and client is linux telnet. Have you tried different clients?

      This is the server script that i am running on a windows machine. My application running on a linux box comes and tries to send a request and my server script listens for requests and then should respond back.

      I dont know why i dont get the whole request.

      Is there a way, i can send the snapshot of the network capture?

      Because, when my application sends the request to a real server, it sends it as 2 packets on the same socket.

        So, your server is on Windows and client on linux. Try adding
        local($/) = "\012";
        in the beginning of your code. http://perldoc.perl.org/perlport.html#Newlines
Re: My server doesnt get all the data sent by the client application
by Anonymous Monk on May 10, 2013 at 22:33 UTC

    readline waits for a trailing newline

    A better protocol would start with the number of bytes of the message, so that you can use read until you read that number of bytes

Re: My server doesnt get all the data sent by the client application
by jakeease (Friar) on May 12, 2013 at 08:33 UTC

    I tweaked a couple things; can't really test except simple localhost things.

    #!/usr/bin/perl -w use strict; use IO::Socket; my $sock = IO::Socket::INET->new( #LocalHost => '168.159.250.206', LocalPort => '80', Proto => 'tcp', Listen => SOMAXCONN, #whatever the + system max is Reuse => 1, ); #$sock->sockopt(SO_RCVBUF, 1500); die $@ unless $sock; # $@ will tell you the error message # my $new_sock = $sock->accept(); # my $new_sock; my $a = ""; my $data = ""; while(my $new_sock = $sock->accept()) { $data = <$new_sock>; #$new_sock->autoflush(1); $| = 1; print $data; $a .= $data; print $a; $| = 0; } print "a is $a"; #close($sock);

      I was working on something else and gone for an hour and i see that i get all of the 2nd buffer. Not sure, why it took so long to get the data. I changed one thing in the code below

      #!/usr/bin/perl -w local($/) = "\012"; use strict; use IO::Socket; my $sock = IO::Socket::INET->new( #LocalHost => '168.159.250.206', LocalPort => '5988', Proto => 'tcp', Listen => SOMAXCONN, #whatever the + + system max is Reuse => 1, ); #$sock->sockopt(SO_RCVBUF, 1500); die $@ unless $sock; # $@ will tell you the error message # my $new_sock = $sock->accept(); # my $new_sock; my $a = ""; my $b = ""; my $data = ""; while(my $new_sock = $sock->accept()) { while(<$new_sock>) {$b = $b.$_; } print $b; #$new_sock->autoflush(1); $| = 1; print $data; $a .= $data; print $a; $| = 0; } print "a is $a"; #close($sock);

      Do you think I cant get an xml content from $_? I know my program has received the below data.its in xml format. Would that be contributing to the problem?

      <CIM CIMVERSION="2.0" DTDVERSION="2.0"><MESSAGE ID="425381" PROTOCOLVERSION="1.0"><SIMPLEREQ><IMETHODCALL NAME="EnumerateInstances"><LOCALNAMESPACEPATH><NAMESPACE NAME="root"/><NAMESPACE NAME="brocade1"/></LOCALNAMESPACEPATH><IPARAMVALUE NAME="ClassName"><CLASSNAME NAME="Brocade_Fabric"/></IPARAMVALUE><IPARAMVALUE NAME="LocalOnly"><VALUE>true</VALUE></IPARAMVALUE><IPARAMVALUE NAME="DeepInheritance"><VALUE>false</VALUE></IPARAMVALUE><IPARAMVALUE NAME="IncludeQualifiers"><VALUE>false</VALUE></IPARAMVALUE><IPARAMVALUE NAME="IncludeClassOrigin"><VALUE>true</VALUE></IPARAMVALUE></IMETHODCALL></SIMPLEREQ></MESSAGE></CIM>

        I receive this data almost after an hour. I dont get it. I don't know why we cant get the data right away. I took a network capture and do see the buffers being sent almost instantaneously.