ipentinmaki has asked for the wisdom of the Perl Monks concerning the following question:

The customer I am working with uses sockets in a way I am not used to.
I need to behave as a client program, I read data from their socket (host/port on their machine) and respond with an ack.
When I want to send the data I do the usual and write to their host/port and wait for an ack.
Using the following simple read I continually see the most recent write by them on their port till the replace it with new data. Is that expected or am I doing something wrong?
#!/usr/local/bin/perl use strict; use Socket; use IO::Handle; my $prot = getprotobyname('tcp'); my $host = inet_aton("localhost"); socket(SOCK, AF_INET, SOCK_STREAM, $prot) or die("socket failed to sta +rt"); my $dest_addr = sockaddr_in(5003, $host); connect(SOCK, $dest_addr) or die("Connect failed"); my $data; while (1) { print STDERR "do a read\n"; sysread(SOCK, $data, 1024); # reads the data on the serve +rs socket # but when loop around again +the data is still there as server # has not written anything ne +w print STDERR "data was $data\n"; sleep 10; } close SOCK;

Replies are listed 'Best First'.
Re: Read Socket on Server
by Illuminatus (Curate) on Sep 29, 2008 at 22:07 UTC
    You are not checking the result of the sysread. To do so could be enlightening...
      It contains the length of the message. In this case 62 on each read.
      The full program does the if defined, if rc>0 and $!==EWOULDBLOCK for a non-blocking socket.
      Speaking of non-blocking I am testing on WinXP. Is it another aberration of windows and I should switch to testing on unix (which is where the client will be).
        Do you actually get the output you describe from the posted version or only the non-blocking version? Something is seriously messed up to get the behaviour you described from the code you posted. It makes more sense for the non-blocking version since you never clear $data.

        Update: To clarify, by "you never clear $data", I mean you rely on sysread to clear $data, and it's entirely reasonable to fit to not do so when it detects there's nothing to read.

Re: Read Socket on Server
by kubrat (Scribe) on Sep 30, 2008 at 22:22 UTC

    What happens if you connect to the server using telnet?