Pb with IO::Socket::INET and recv

Discussion in 'Perl Misc' started by Sébastien Cottalorda, Sep 5, 2003.

  1. Hi all,
    Since I migrate a client program to Mandrake 9.1 (kernel 2.4.21-0.13) and
    perl 5.8.0, this little program didn't work.
    The problem occured on the
    unless(recv...) line
    It always return me "false" => my program think that there was an error.
    If I remove the unless (recv...) check, it seems to work.


    Thanks in advance for any kind of help.

    Sébastien



    #!/usr/bin/perl -w
    use IO::Socket;
    use IO::Select;
    use strict;
    my $end_car="\r";
    my $time_out_sending = my $time_out_receiving = 30;
    my @recep = &envoie("Hello, How are you","192.168.0.1","45678","");
    foreach (@recep){ print "$_\n"}
    exit;


    sub envoie {
    my $to_send = $_[0];
    my $adr_ip= $_[1];
    my $por = $_[2];
    my $socket='';
    #=========================/ Socket creation \=========================
    unless ($socket = IO::Socket::INET->new(PeerAddr=> $adr_ip,
    PeerPort=> $por,
    Proto=> "tcp",
    Timeout=>10,
    Type=> SOCK_STREAM))
    {
    return "Cannot connect to $adr_ip:$por $@";
    }
    my $s=IO::Select->new();
    $s->add($socket);
    #==========================/ Sending \================================
    if ($s->can_write($time_out_sending)){
    unless ($socket->send("$to_send"."$end_car",'')){ #>>>> SENDING
    $s->remove($socket);
    close($socket);
    return "Cannot Send $!";
    }
    }
    else { #------------> Time out <-------------
    $s->remove($socket);
    close($socket);
    return "TIME OUT sending datas";
    }
    #=========================/ Receiving \===============================
    my $data_read="";
    if ($s->can_read($time_out_receiving)){
    unless ($socket->recv($data_read,'1024')){ #<<<<< PROB HERE
    $s->remove($socket); # Always executed
    close($socket); # " "
    return "Cannot Receive $!"; # " "
    }
    $s->remove($socket);
    close($socket);
    return $data_read;
    }
    else { #------------> Time out <-------------
    $s->remove($socket);
    close($socket);
    return "TIME OUT receiving datas";
    }
    }

    --
    [ retirer NOSPAM pour répondre directement
    remove NOSPAM to reply directly ]
    Sébastien Cottalorda, Sep 5, 2003
    #1
    1. Advertising

  2. Sébastien Cottalorda wrote:

    > Hi all,
    > Since I migrate a client program to Mandrake 9.1 (kernel 2.4.21-0.13) and
    > perl 5.8.0, this little program didn't work.
    > The problem occured on the
    > unless(recv...) line
    > It always return me "false" => my program think that there was an error.
    > If I remove the unless (recv...) check, it seems to work.
    >
    >
    > Thanks in advance for any kind of help.
    >
    > Sébastien
    >
    >
    >
    > #!/usr/bin/perl -w
    > use IO::Socket;
    > use IO::Select;
    > use strict;
    > my $end_car="\r";
    > my $time_out_sending = my $time_out_receiving = 30;
    > my @recep = &envoie("Hello, How are you","192.168.0.1","45678","");
    > foreach (@recep){ print "$_\n"}
    > exit;
    >
    >
    > sub envoie {
    > my $to_send = $_[0];
    > my $adr_ip= $_[1];
    > my $por = $_[2];
    > my $socket='';
    > #=========================/ Socket creation \=========================
    > unless ($socket = IO::Socket::INET->new(PeerAddr=> $adr_ip,
    > PeerPort=> $por,
    > Proto=> "tcp",
    > Timeout=>10,
    > Type=> SOCK_STREAM))
    > {
    > return "Cannot connect to $adr_ip:$por $@";
    > }
    > my $s=IO::Select->new();
    > $s->add($socket);
    > #==========================/ Sending \================================
    > if ($s->can_write($time_out_sending)){
    > unless ($socket->send("$to_send"."$end_car",'')){ #>>>> SENDING
    > $s->remove($socket);
    > close($socket);
    > return "Cannot Send $!";
    > }
    > }
    > else { #------------> Time out <-------------
    > $s->remove($socket);
    > close($socket);
    > return "TIME OUT sending datas";
    > }
    > #=========================/ Receiving \===============================
    > my $data_read="";
    > if ($s->can_read($time_out_receiving)){
    > unless ($socket->recv($data_read,'1024')){ #<<<<< PROB HERE
    > $s->remove($socket); # Always executed
    > close($socket); # " "
    > return "Cannot Receive $!"; # " "
    > }
    > $s->remove($socket);
    > close($socket);
    > return $data_read;
    > }
    > else { #------------> Time out <-------------
    > $s->remove($socket);
    > close($socket);
    > return "TIME OUT receiving datas";
    > }
    > }
    >


    Solved problem !!!!

    unless (defined $socket->recv($data_read, 1024, 0));

    Thanks to Paul GABORIT.

    Sébastien


    --
    [ retirer NOSPAM pour répondre directement
    remove NOSPAM to reply directly ]
    Sébastien Cottalorda, Sep 16, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rad
    Replies:
    0
    Views:
    540
  2. Aggro

    Re: INET socket programming questions

    Aggro, Jun 28, 2003, in forum: C Programming
    Replies:
    1
    Views:
    1,208
    Artemio
    Jun 29, 2003
  3. deepak nayak
    Replies:
    3
    Views:
    411
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
    Oct 25, 2007
  4. Joe
    Replies:
    10
    Views:
    268
  5. DJ Stunks
    Replies:
    11
    Views:
    455
Loading...

Share This Page