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. Advertisements

  2. Hobbit HK

    kz Guest

    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. Advertisements

  3. Hobbit HK

    kz Guest

    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

    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.
    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
     
    Ben Morrow, Feb 9, 2004
    #4
  5. Hobbit HK

    Hobbit HK Guest

    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...
    What do you mean by buffer reads? read($sock, $buffer, 1000)?
    And "push a :unix"?
     
    Hobbit HK, Feb 10, 2004
    #5
  6. Hobbit HK

    Ben Morrow Guest

    No it doesn't. It should return when there's data, or when the timeout
    has expired, whichever is the sooner.
    Yup. Use sysread instead.
    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
     
    Ben Morrow, Feb 10, 2004
    #6
  7. Hobbit HK

    Hobbit HK Guest

    Right... I don't know why it did before (or why I thought it did)...
    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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.