Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Answer: How do I send a file through a Socket Connection ?

( #6017=categorized answer: print w/ replies, xml ) Need Help??

Q&A > network programming > How do I send a file through a Socket Connection ? contributed by btrott

The client shouldn't be calling accept, should it? The server should, in order to accept new clients.

Here's a really simplistic example of sending a file across a socket. Here's the server:

use IO::Socket; my $server = IO::Socket::INET->new( Listen => 5, LocalAddr => 'localhost', LocalPort => 5050, Proto => 'tcp' ) or die "Can't create server socket: $!"; my $client = $server->accept; open FILE, ">out" or die "Can't open: $!"; while (<$client>) { print FILE $_; } close FILE;
And here's the client:
use IO::Socket; my $server = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => 5050, Proto => 'tcp' ) or die "Can't create client socket: $!"; open FILE, "in"; while (<FILE>) { print $server $_; } close FILE;
This should get you started. Just start up the server in the background, then run the client, and it should send the contents of "in" to "out", over the socket.

You'll probably want to tinker around with it a bit--perhaps put in some command line options for the host and port; make it so that the server handles multiple connections (using IO::Select); etc.

Comment on Answer: How do I send a file through a Socket Connection ?
Select or Download Code
RE: Answer: How do I send a file through a Socket Connection ?
by chromatic (Archbishop) on Mar 24, 2000 at 08:40 UTC
    You're right, I'm really lame today. Plus, I can't seem to edit my existing nodes for some reason. Maybe vroom should take the bit about filehandles out of my answer and the code out of yours and call it good on this one. :)
Re: Answer: How do I send a file through a Socket Connection ?
by Valerie170 (Initiate) on Jan 21, 2002 at 21:16 UTC
    In the Client and the server respectively-if they exist on different hosts, then what are the values of 'localhost' for each? Also, I can't see where the client code actually makes contact with the server? Would really appreciate a fairly rapid answer. Thanks in advance.
Re: Answer: How do I send a file through a Socket Connection ?
by Anonymous Monk on Jan 08, 2003 at 22:17 UTC
    I've used snippets of this code for a speedy little internal file server for a while (albeit the reverse direction of the example). We just installed a fresh RedHat 8.0/perl5.8.0/dual athlon box, and for some reason, the client always corrupts the file. The file is always 50% larger than the original. It also seems the garbage appears in the same offsets in the file, but the garbage is different each time. Any idea why such simple code broke? joel@acdstar.com
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-12-26 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls