Solved

Socket receive

Posted on 2004-10-30
199 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
Question by:MKrauss
    9 Comments
     
    LVL 1

    Author Comment

    by:MKrauss
    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
    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
    Mkrauss,
    Do you want the token as part of your record or just want to lose it??

    Manav
    0
     
    LVL 1

    Author Comment

    by:MKrauss
    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
    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
    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
    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:
    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
    That makes it, many thanks !
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    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…
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    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…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now