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

Hi Monks,
I am using a socket connection to read dynamic data and store this in buffer.I am using sysread() to do so.But since the data recieved is very inconsistent i.e, it is possible that we recieve data or not recieve data.in that case the filehandle reads the same data again when there is no new data. Please let me know is there a way not to read the same data we read earlier. I am pasting the code below. Please revert back if you need more info. To read i use: my $nread=sysread($this->{fh},$this->{buf},16384); I put this line in a loop to read from socket for 3 secs and also till it really has something to read. Appriciate an early response

Replies are listed 'Best First'.
Re: Reading froma dynamic file handle
by Zaxo (Archbishop) on Nov 22, 2005 at 05:57 UTC

    Your handle is a socket, so you should use recv to read from it. Rather than polling the handle, try four-arg select to allow sleep between reads. For sockets you can even do that on winders.

    You can simplify usage with IO::Socket and IO::Select. It's a worthwhile exercise to code a few applications from perl builtins, though.

    After Compline,
    Zaxo

Re: Reading froma dynamic file handle
by mikeock (Hermit) on Nov 22, 2005 at 16:11 UTC
    I always loop through a socket connection to get the data that I want. my code would be as follows:

    Code is untested

    #!/user/bin/perl -w use strict; use IO::Socket::INET; sub sock_connection{ my $sockname = new IO::Socket::INET(PeerAddr => "putAddrHere", PeerPort => "Port", Proto => "tcp") or die "cannot +connect to host: $!"; print $sockname "@_" # Puts data passed to sub shutdown($sockname, 1); while(<$sockname>){ # place regex to remove prompt message # Display msgs using print $_; } undef $sockname }

    if the code is called in a subroutine, like the above, you can then call the subroutine to loop through the code that you want to send to the socket.

    sock_connection("my command here")

    Hope this helps?!

    Edit:Added the undef to clear the socket connection when done.

A reply falls below the community's threshold of quality. You may see it by logging in.