Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by btrott (Parson)
on Mar 24, 2000 at 04:13 UTC ( [id://6017]=note: print w/replies, xml ) Need Help??


in reply to How do I send a file through a Socket Connection ?

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.

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://6017]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-18 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found