Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Socket receive

Posted on 2004-10-30
9
Medium Priority
?
201 Views
Last Modified: 2010-03-05
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 ?
0
Comment
Question by:MKrauss
  • 4
  • 4
9 Comments
 
LVL 1

Author Comment

by:MKrauss
ID: 12453122
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
 
LVL 18

Expert Comment

by:kandura
ID: 12457627
If you set

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

you could then safely use

    my $line = $socket->getline;

0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12462526
Mkrauss,
Do you want the token as part of your record or just want to lose it??

Manav
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:MKrauss
ID: 12464668
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
 
LVL 18

Expert Comment

by:kandura
ID: 12464713
oh dear! my mistake.

It's either

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

or

    local $/ = 'test-end';

0
 
LVL 1

Author Comment

by:MKrauss
ID: 12465585
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
 
LVL 18

Expert Comment

by:kandura
ID: 12472670
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
 
LVL 18

Accepted Solution

by:
kandura earned 375 total points
ID: 12472690
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
 
LVL 1

Author Comment

by:MKrauss
ID: 12476195
That makes it, many thanks !
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question