in reply to Re: Sockets + Read
in thread Sockets + Read

I have no real way to determine when the data is finished being read, other than there is nothing else coming in. Basically, I want to read the data from the client, act on it and return the results to the client and then have the server terminate the connection. (Using sysread in this example, because according to "Network Programming with Perl, page 359" it is the safest option with a nonblocking socket)

I've modified the code in the following way:
($client, my $client_addr) = $server->accept( ); my $result = fcntl($client, F_SETFL, O_NONBLOCK); my $read = ""; my $buffer; print "DEBUG Point: 1\n"; while (sysread($client,$buffer,1024)) { print "DEBUG Point: 2\n"; $read .= $buffer; } print "DEBUG Point: 3\n"; print "$read\n";
This is a step closer. It exits the loop and prints the data ($read) to the terminal window.

Problem is, i am no longer seeing all of the data. With the original code from the first post I saw all of the data. The only problem was that it was not leaving the while loop after reading all of the data in. Now it seems to be leaving off the last few lines (i'm sending 14 lines of text through and only seeing 9 being printed to the terminal)

Replies are listed 'Best First'.
Re^3: Sockets + Read
by dave_the_m (Monsignor) on Aug 18, 2005 at 09:35 UTC
    I have no real way to determine when the data is finished being read, other than there is nothing else coming in.
    In that case, if you think about it logically, there is no reliable way for your server to know when the client has finished sending. All proper network protocols give some way for the server to know, eg, client closes connection, client sends an agreed, fixed size of data, client sends a terminator code (\r\n in HTTP), client sends a header that contains the length of the rest of the message etc.

    I think you need to rethink your protocol.

    Dave.