Socket receive

I need to send testqueries via socket and i dont know the length of the returned
datas, i dont get CR in the received string but i know the end token.
How can i do that ?
LVL 1
MKraussAsked:
Who is Participating?
 
kanduraCommented:
Note that I said IO::Socket->input_record_separator.
It also works with IO::Handle (since IO::Socket inherits from IO::Handle), but I think this is more readable.
0
 
MKraussAuthor Commented:
In addition i would need to set a timeout, this is what i have, but
i'm not happy with this because i assume a recied size and i
dont have a proper timeout .

use IO::Socket;
$socket = IO::Socket::INET->new(PeerAddr => "testhost",
                        PeerPort => "4000",
                        Proto    => "tcp",
                        Type     => SOCK_STREAM
                                Timeout   => 5 )
or die "No Connection";

print $socket "Testquery";
$socket->recv($text,8192);
print "\nReceived message '", $text,"'\n";
close($socket);

0
 
kanduraCommented:
If you set

    local IO::Handle->input_record_separator( "your_token_here" );

you could then safely use

    my $line = $socket->getline;

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
manav_mathurCommented:
Mkrauss,
Do you want the token as part of your record or just want to lose it??

Manav
0
 
MKraussAuthor Commented:
This looks interesting, however, using the sample results in "Can't modify non-lvalue subroutine call at ..." the line
was: local IO::Handle->input_record_separator( "~~" );

In the meantime i fixed my timeout problem, any chance to set the input_record_separator ?
the script looks now:

$socket = new IO::Socket::INET(
         PeerAddr => $UsedHosts,
         PeerPort => $UsedPorts,
         Proto    => "tcp",
         Type     => SOCK_STREAM)
   or &ErrorHandler;
   #Add a IO::select handle to read from socket  
   $sel = new IO::Select( $socket );
   $sel->add($UsedHosts);
   #Send Datas
   $socket->send ($Sendstring);
   #Read from handle within the given timeout !
   @read_from = $sel->can_read($ReceivedTimeout);
   $RecData="";  #Reset Datas
   foreach $sck (@read_from) {
    $socket->recv($RecData,8192,0);
    #local IO::Handle->input_record_separator( "test-end" );  #Error at this point
    #my $line = $socket->getline;
   }
   if (length($RecData) eq 0) {
   ........
   

0
 
kanduraCommented:
oh dear! my mistake.

It's either

    IO::Handle->input_record_separator( "test-end" );

or

    local $/ = 'test-end';

0
 
MKraussAuthor Commented:
That doesnt help either, not sure how to deal with the handle class, the token i need to identify
the end of the received string, from the above sampe i've tried

IO::Handle->input_record_separator( "END-TOKEN" )
my $line = $socket->getline;
or
$io = new IO::Handle;
if (IO::Handle->input_record_separator( "END-TOKEN" )) {
my $line = $socket->getline;
.....
}
or
$io = new IO::Handle;
$io->input_record_separator( "END-TOKEN" );
my $line = $socket->getline;

With those samples i don't get the receive string, any idea or help would be great, will learn more about io::handlle anyway.
0
 
kanduraCommented:
Here's a sample script. It will fetch the homepage of www.microsoft.com, but considers the line ending to be '</'.

    use IO::Socket;
    IO::Socket->input_record_separator( "</" );
   
    $socket = IO::Socket::INET->new(PeerAddr => "www.microsoft.com",
                        PeerPort => "80",
                        Proto    => "tcp",
                        Type     => SOCK_STREAM
                                    Timeout   => 5 )
    or die "No Connection";
   
    print $socket "GET / HTTP/1.0\r\n\r\n";

    while(my $l = $socket->getline) {
        print "\nReceived message '", $l,"'\n";
    }
    close($socket);


A snippet from the sample output is:

Received message 'title><style type="text/css" media="all">@import "/h/en-us/r/hp.css";</'

Received message 'style><script type="text/javascript" src="/h/en-us/r/hp.js"></'

Received message 'script><script type="text/javascript" src="/h/en-us/r/mshome_popup_Q4.js"></'

Received message 'script></'
0
 
MKraussAuthor Commented:
That makes it, many thanks !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.