Autoflush and new/read-line not working as expected/documented?

Discussion in 'Perl Misc' started by jk, Jan 5, 2009.

  1. jk

    jk Guest

    Hello, I am trying to write a client-server program. Without going
    into details, I'm simply reading the terminal on the server and
    sending the contents to the client. I'm having an issue where neither
    the client nor server are autoflushing their sockets by default, and
    the client won't readlines from the server, they come as a big chunk
    only after the server fails.

    I have the same issue on Mac OS 10.5 (perl: 5.8.8) and Ubuntu (kernel
    2.6.27) (perl: 5.10.0)

    Here is some base code:
    tserver.pl:
    #! /usr/bin/perl -w
    use strict;
    use Socket;
    use IO::Handle;

    my $port = shift || 7890;

    socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die
    "socket: $!";
    setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";
    bind(SERVER, sockaddr_in($port, INADDR_ANY)) or die "bind: $!";
    listen(SERVER, SOMAXCONN) or die "listen: $!";
    print "SERVER started on port $port\n";

    while(not accept(CLIENT, SERVER)){}
    if(fork()){
    CLIENT->autoflush(1);
    print CLIENT "Who is this?\n";
    my $line = <CLIENT>;
    print $line;
    my ($rpid, $rid, $rhost) = split(/:/, $line, 3);
    print CLIENT "Hello, $rid\n";
    } else {
    while(<> ne "quit"){}
    }

    wait();
    close CLIENT;

    tclient.pl:
    #! /usr/bin/perl -w
    use strict;
    use Socket;
    use FileHandle;

    my $host = shift || 'localhost';
    my $port = shift || 7890;

    socket(SOCKET, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die
    "socket: $!";
    connect(SOCKET, sockaddr_in($port, inet_aton($host)) ) or die
    "connect: $!";
    SOCKET->autoflush(1);

    print SOCKET "JIM:TEST:".`hostname`."\n";
    print SOCKET "WHOOO\n";
    print <SOCKET>;
    close SOCKET or die "close: $!"


    If the first autoflush is removed (in the server) no text appears on
    the client if the server is killed. If the autodlush in the client is
    removed, no text appears on the server. Ok, so that's not what the
    docs say (sockets should autoflush, right?) but I'm sure this is a
    weird, well know exception, I can live with that, but I'd like to know
    what the exception is.

    The other problem is that the print <SOCKET> in the client prints both
    lines, and blocks until the connection is lost. The $line = <CLIENT>
    line in the server correctly returns the first line sent from the
    client, immediately and solely. Why doesn't the client correctly read
    lines from the server, and instead block until the connection is lost.

    Thanks so much for any help. I've been banging my head against my
    desk for a few days looking at docs and forums. I'm sorry if this is
    just some small thing I'm not seeing.

    Thanks,
    Jim
    jk, Jan 5, 2009
    #1
    1. Advertising

  2. jk

    Guest

    jk <> wrote:
    > Hello, I am trying to write a client-server program. Without going
    > into details, I'm simply reading the terminal on the server and
    > sending the contents to the client. I'm having an issue where neither
    > the client nor server are autoflushing their sockets by default, and
    > the client won't readlines from the server, they come as a big chunk
    > only after the server fails.
    >
    > I have the same issue on Mac OS 10.5 (perl: 5.8.8) and Ubuntu (kernel
    > 2.6.27) (perl: 5.10.0)
    >
    >
    > socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die
    > "socket: $!";


    I'd recommend using IO::Select, or at least using lexical file handles.

    .....
    >
    > If the first autoflush is removed (in the server) no text appears on
    > the client if the server is killed. If the autodlush in the client is
    > removed, no text appears on the server. Ok, so that's not what the
    > docs say (sockets should autoflush, right?)


    The only place I know of where the docs say that is in IO::Socket. You
    are not using IO::Socket. You are bypassing IO::Socket by invoking
    the low-level functions directly instead, so you do not get the behavior
    of IO::Socket when you are not using it.


    > The other problem is that the print <SOCKET> in the client prints both
    > lines, and blocks until the connection is lost. The $line = <CLIENT>
    > line in the server correctly returns the first line sent from the
    > client, immediately and solely. Why doesn't the client correctly read
    > lines from the server, and instead block until the connection is lost.


    print imposes a list context on the thing expression it is to print. In
    a list context, <SOCKET> will return *all* the lines on the handle, not
    just the next one. In order to do that, it will block until it gets an
    eof.

    Perhaps you meant:
    print scalar <SOCKET>;

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Jan 6, 2009
    #2
    1. Advertising

  3. jk

    jk Guest

    Well, that worked. ::face-palm:: sorry it was a stupid issue.

    I misread the IO::Socket page:-\ I should prob switch this over to be
    more OO once it works, huh?

    Thanks so much,
    Jim
    jk, Jan 6, 2009
    #3
    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. Herman Chan

    CGI autoflush in Window

    Herman Chan, Oct 9, 2003, in forum: Perl
    Replies:
    0
    Views:
    561
    Herman Chan
    Oct 9, 2003
  2. kaushikshome
    Replies:
    4
    Views:
    743
    kaushikshome
    Sep 10, 2006
  3. scad
    Replies:
    23
    Views:
    1,129
    Alf P. Steinbach
    May 17, 2009
  4. cyberco

    File.read not documented

    cyberco, Jan 23, 2005, in forum: Ruby
    Replies:
    6
    Views:
    94
    Aredridel
    Jan 23, 2005
  5. Mason Kelsey
    Replies:
    11
    Views:
    183
    Mason Kelsey
    Sep 24, 2009
Loading...

Share This Page