IO::Socket::INET Problem

Discussion in 'Perl Misc' started by Hobbit HK, Feb 9, 2004.

  1. Hobbit HK

    Hobbit HK Guest

    I'm using WinXP (although I also have RH Linux and there's no
    difference) and I want to write a script which uses sockets... Now, my
    main problem is when there's nothing else to read from the socket...
    That's where my script stucks... My guess is that because it keeps on
    waiting for something (it can still get data)... Is there some way to
    bypass this?
    Will Blocking=>0 in IO::Socket::INET's new() help?

    BTW
    I'm using $line=<$sock> to read from the socket.
     
    Hobbit HK, Feb 9, 2004
    #1
    1. Advertising

  2. Hobbit HK

    kz Guest

    "Hobbit HK" <> wrote in message
    news:...
    > I'm using WinXP (although I also have RH Linux and there's no
    > difference) and I want to write a script which uses sockets... Now, my
    > main problem is when there's nothing else to read from the socket...
    > That's where my script stucks... My guess is that because it keeps on
    > waiting for something (it can still get data)... Is there some way to
    > bypass this?
    > Will Blocking=>0 in IO::Socket::INET's new() help?
    >
    > BTW
    > I'm using $line=<$sock> to read from the socket.


    Your example seems to be blocking.
    Snippet below does not exactly cover your case, worked in mine, though.
    Calling the can_read method with no args was blocking. Specifying 500 ms
    wasn't.

    my $listen = IO::Socket::INET->new(Listen => 5, LocalAddr => 'localhost',
    LocalPort => 12300, Proto => 'tcp') or die "listen: $!";
    my $select = new IO::Select() or die "Can't setup main selector: $!";
    $select->add($listen);
    while (1) {
    my @ready = $select->can_read(500); # magic
    foreach my $endpt (@ready) {
    # do whatever needs to be done
    } }

    HTH,

    Zoltan
     
    kz, Feb 9, 2004
    #2
    1. Advertising

  3. Hobbit HK

    kz Guest

    "Hobbit HK" <> wrote in message
    news:...
    > I'm using WinXP (although I also have RH Linux and there's no
    > difference) and I want to write a script which uses sockets... Now, my
    > main problem is when there's nothing else to read from the socket...
    > That's where my script stucks... My guess is that because it keeps on
    > waiting for something (it can still get data)... Is there some way to
    > bypass this?
    > Will Blocking=>0 in IO::Socket::INET's new() help?
    >
    > BTW
    > I'm using $line=<$sock> to read from the socket.


    Your example seems to be blocking.
    Snippet below does not exactly cover your case, worked in mine, though.
    Calling the can_read method with no args was blocking. Specifying 500 ms
    wasn't.

    my $listen = IO::Socket::INET->new(Listen => 5, LocalAddr => 'localhost',
    LocalPort => 12300, Proto => 'tcp') or die "listen: $!";
    my $select = new IO::Select() or die "Can't setup main selector: $!";
    $select->add($listen);
    while (1) {
    my @ready = $select->can_read(500); # magic
    foreach my $endpt (@ready) {
    # do whatever needs to be done
    } }

    HTH,

    Zoltan
     
    kz, Feb 9, 2004
    #3
  4. Hobbit HK

    Ben Morrow Guest

    (Hobbit HK) wrote:
    > I'm using WinXP (although I also have RH Linux and there's no
    > difference) and I want to write a script which uses sockets... Now, my
    > main problem is when there's nothing else to read from the socket...
    > That's where my script stucks... My guess is that because it keeps on
    > waiting for something (it can still get data)... Is there some way to
    > bypass this?


    What do you want it to do in that case? Stop? It is usual for socket
    programs to either wait until the other end closes the socket (which
    will come out as reading EOF) or to close it yourself on some
    condition, ie. during the processing immediately after having read something.

    > Will Blocking=>0 in IO::Socket::INET's new() help?
    > I'm using $line=<$sock> to read from the socket.


    Might do. It won't if you use buffered reads, though, so you either
    want to switch to sysread or (if you have 5.8) push a :unix (NB. I
    have never tried this and, though it ought to work, I don't really
    know... :). You may also, as someone else said, want to use
    IO::Select.

    Ben

    --
    'Deserve [death]? I daresay he did. Many live that deserve death. And some die
    that deserve life. Can you give it to them? Then do not be too eager to deal
    out death in judgement. For even the very wise cannot see all ends.'
    :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
     
    Ben Morrow, Feb 9, 2004
    #4
  5. Hobbit HK

    Hobbit HK Guest

    Ben Morrow <> wrote in message news:<c085k2$h2n$>...
    > (Hobbit HK) wrote:
    > > I'm using WinXP (although I also have RH Linux and there's no
    > > difference) and I want to write a script which uses sockets... Now, my
    > > main problem is when there's nothing else to read from the socket...
    > > That's where my script stucks... My guess is that because it keeps on
    > > waiting for something (it can still get data)... Is there some way to
    > > bypass this?

    >
    > What do you want it to do in that case? Stop? It is usual for socket
    > programs to either wait until the other end closes the socket (which
    > will come out as reading EOF) or to close it yourself on some
    > condition, ie. during the processing immediately after having read something.
    >


    Yes, I want it to stop reading and move on... After a while when no
    data has arrived to just stop reeading... It's kinda annoying with
    IO::Select because it still waits the number of seconds I told it,
    even if there's data...

    > > Will Blocking=>0 in IO::Socket::INET's new() help?
    > > I'm using $line=<$sock> to read from the socket.

    >
    > Might do. It won't if you use buffered reads, though, so you either
    > want to switch to sysread or (if you have 5.8) push a :unix (NB. I
    > have never tried this and, though it ought to work, I don't really
    > know... :).


    What do you mean by buffer reads? read($sock, $buffer, 1000)?
    And "push a :unix"?

    >You may also, as someone else said, want to use
    > IO::Select.


    > Ben
     
    Hobbit HK, Feb 10, 2004
    #5
  6. Hobbit HK

    Ben Morrow Guest

    (Hobbit HK) wrote:
    > Ben Morrow <> wrote in message
    > news:<c085k2$h2n$>...
    > > What do you want it to do in that case? Stop? It is usual for socket
    > > programs to either wait until the other end closes the socket (which
    > > will come out as reading EOF) or to close it yourself on some
    > > condition, ie. during the processing immediately after having read something.
    > >

    >
    > Yes, I want it to stop reading and move on... After a while when no
    > data has arrived to just stop reeading... It's kinda annoying with
    > IO::Select because it still waits the number of seconds I told it,
    > even if there's data...


    No it doesn't. It should return when there's data, or when the timeout
    has expired, whichever is the sooner.

    > > > Will Blocking=>0 in IO::Socket::INET's new() help?
    > > > I'm using $line=<$sock> to read from the socket.

    > >
    > > Might do. It won't if you use buffered reads, though, so you either
    > > want to switch to sysread or (if you have 5.8) push a :unix (NB. I
    > > have never tried this and, though it ought to work, I don't really
    > > know... :).

    >
    > What do you mean by buffer reads? read($sock, $buffer, 1000)?


    Yup. Use sysread instead.

    > And "push a :unix"?


    If you are using 5.8 and built your perl with PerlIO,

    binmode $FH, ':unix';

    *ought* to make all reads on that FH unbuffered, even those done with
    <>... as I said, though, I've not tried it, and I wouldn't be
    surprised if it didn't work. See perldoc PerlIO.

    Ben

    --
    perl -e'print map {/.(.)/s} sort unpack "a2"x26, pack "N"x13,
    qw/1632265075 1651865445 1685354798 1696626283 1752131169 1769237618
    1801808488 1830841936 1886550130 1914728293 1936225377 1969451372
    2047502190/' #
     
    Ben Morrow, Feb 10, 2004
    #6
  7. Hobbit HK

    Hobbit HK Guest

    Ben Morrow <> wrote in message news:<c0am1u$727$>...
    > > Yes, I want it to stop reading and move on... After a while when no
    > > data has arrived to just stop reeading... It's kinda annoying with
    > > IO::Select because it still waits the number of seconds I told it,
    > > even if there's data...

    >
    > No it doesn't. It should return when there's data, or when the timeout
    > has expired, whichever is the sooner.
    >


    Right... I don't know why it did before (or why I thought it did)...

    > > > > Will Blocking=>0 in IO::Socket::INET's new() help?
    > > > > I'm using $line=<$sock> to read from the socket.
    > > >
    > > > Might do. It won't if you use buffered reads, though, so you either
    > > > want to switch to sysread or (if you have 5.8) push a :unix (NB. I
    > > > have never tried this and, though it ought to work, I don't really
    > > > know... :).

    [snip]
    >
    > binmode $FH, ':unix';
    >
    > *ought* to make all reads on that FH unbuffered, even those done with
    > <>... as I said, though, I've not tried it, and I wouldn't be
    > surprised if it didn't work. See perldoc PerlIO.
    >
    > Ben


    binmode $sock, ':unix' didn't work... At least I saw no change...
    I hope IO::Selcet will do the trick...
     
    Hobbit HK, Feb 12, 2004
    #7
    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:
    551
  2. Aggro

    Re: INET socket programming questions

    Aggro, Jun 28, 2003, in forum: C Programming
    Replies:
    1
    Views:
    1,216
    Artemio
    Jun 29, 2003
  3. Joe
    Replies:
    10
    Views:
    285
  4. Lucas Young

    Problem with Perl DBI - IO::Socket::INET:

    Lucas Young, Dec 11, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    466
    Lucas Young
    Dec 14, 2005
  5. Stuart Gall

    IO::Socket::INET on OSX or TCP stack problem

    Stuart Gall, May 7, 2009, in forum: Perl Misc
    Replies:
    17
    Views:
    379
    Stuart Gall
    May 15, 2009
Loading...

Share This Page