Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

No data in server file using io::socket

by wileykt (Acolyte)
on Oct 25, 2001 at 00:04 UTC ( [id://121257]=perlquestion: print w/replies, xml ) Need Help??

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

I'm new to Perl and I'm working on a client socket program that sends files to a server socket program. The problem I'm having is that the file never gets populated with data on the server. The confusing part is if I change the while loop to while(<$client>) and put the $client = $server->accept(); above the loop, it works. This doesn't do me any good though because I need the server to continuously wait for new files from the client. Thanks for the help.
#!/usr/contrib/bin/perl -w use IO::Socket; use Proc::Daemon; Proc::Daemon::Init; $receivefile = ">test.CONF"; $server = IO::Socket::INET->new( Listen => 5, LocalAddr => 'localhost', LocalPort => 5050, Proto => 'tcp', Reuse => 1, Type => SOCK_STREAM ) or die "Can't create server socket: $!"; open FILE, $receivefile or die "Can't open: $!"; while ($client = $server->accept()) { print FILE $_; } close($client);

Replies are listed 'Best First'.
Re: No data in server file using io::socket
by wog (Curate) on Oct 25, 2001 at 00:12 UTC
    You can nest your connection accepting loop and your connection reading loop:

    while ($client = $server->accept()) { # first get a connection while (<$client>) { # read each line from it print FILE $_; # and write the data to our file } close $client; # then close the connection } # repeat
Re: No data in server file using io::socket
by traveler (Parson) on Oct 25, 2001 at 01:48 UTC
    A couple of notes: 1) wog is correct in his changes, but you need to set the socket option SO_REUSEADDR like this
    $server = ... setsockopt $server, SOL_SOCKET, SO_REUSEADDR, 1;
    or it may be "some time" before the socket is available for reuse. If you are on *NIX see the bind(2) manual. 2) Setting LocalAddr to localhost is probably not what you want. If you want connections from other computers, use INADDR_ANY or just don't set LocalAddr.

    HTH, --traveler

      you need to set the socket option SO_REUSEADDR...

      Using the option Reuse => 1 when creating the socket, wilekt is already setting that option.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found