Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Perl Sockets question

by jimbo (Initiate)
on Feb 08, 2008 at 22:46 UTC ( [id://667096]=perlquestion: print w/replies, xml ) Need Help??

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

HI all.
Firstly, I am not normally a Perl developer...I tinker with it when I need a specific solution....so forgive m if the below is obvious smile

What I have is a Perl script daemon that looks at a file using Tail and then outputs the data via a serial port. This works perfectly fine. BUT, I wanted to ramp it up a bit and instead of outputting the data via serial, I want to send it via TCP.
I googled around, and found some pretty simple code, but I get unexpected results.
Basically, I dont get any data output until I stop the daemon...which I only stumbled upon when testing.
Here is part of my code:-

my $sock = new IO::Socket::INET ( PeerAddr => '192.168.1.2', PeerPort => '8000', Proto => 'tcp' ); while (defined(my $line=$file->read)) { print TTY "$line"; #out serial print $sock "$line"; #out tcp } close($sock);

As you can see, not exactly complex.
I tried moving the "my $sock" init of the socjet to just above the "print $sock" line, then closeing the socket, but this didnt really work. I tried $sock->send(); but this didnt make any difference.

I am sure someone will spot the problem beable to tell.

Thanks, Jeremy

Edit: g0n - code tags & formatting

Replies are listed 'Best First'.
Re: Perl Sockets question
by moritz (Cardinal) on Feb 08, 2008 at 23:01 UTC
    then closeing the socket, but this didnt really work

    What do you mean by "didn't really work"?

    If you open the socket before each line, and close it after the line, each connection will only transport one line. Is that the desired behaviour?

    Edit: what you probably need is autoflushing the $sock. IO::Socket says As of VERSION 1.18 all IO::Socket objects have autoflush turned on by default. This was not the case with earlier releases. So which version of IO::Socket are you using?

    (And please but <code>...</code> tags around your example code.)

      sorry about the bit :-) I am not 100% sure about what version I am using. I am using perl version 5.8.8 if thats any help. how can I tell what version f IO::Socket I am using? If I DO put the open and close around the  print socket$ $line it just outputs empty strings which is what I was meaning byt "didnt really work" Thanks, jeremy
        perl -MIO::Socket -e 'print "$IO::Socket::VERSION\n"'
Re: Perl Sockets question
by zentara (Archbishop) on Feb 09, 2008 at 16:42 UTC
    Try adding a newline after the socket print, that forces it to send(flush). Otherwise, it will tend to accumulate the sent data in a buffer for efficiency. Search groups.google.com for "perl socket flush" for other methods. The newline is the simplest, but there are modules which turn on auto-flushing.
    print $sock $line,"\n"; #out tcp print $sock "$line\n"; #out tcp

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://667096]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-23 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found