Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How do I send a file through a Socket Connection ?

( #5990=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Mar 24, 2000 at 00:18 UTC
Q&A  > network programming


Answer: 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.

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

The IO::Socket package inherits from IO::Handle, so you can treat your socket (after it's all set up) like a filehandle. You can print to it or read from it! Just do something like:

open(INPUT, $my_file) || die "Can't open: $!"; print $connection while (<INPUT); close(INPUT) || die "Can't close: $!";
Disabling buffering ($| = 1) might be a good idea. You can also call select on the $connection object.

On the server side, you can just read from the socket (once it is set up) with the normal angle operators. Using the accept() method will cause the server to listen to the socket until it gets a response, and it will return an object with filehandle properties. Whatever you want to do with it from there is up to you, once it's in a scalar or an array.

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

Sometimes (mainly when you're passing a file from one a process running under one user id to another process under a different user id) it makes sense to pass the filehandle and not the file. To do this, you'd want to use Socket::PassAccessRights as follows:

Socket::PassAccessRights::sendfd(fileno(SOCKET), fileno(FILE_DESCRIPTOR));

On the other end you'd want to receive like:

$file_descriptor = Socket::PassAccessRights::recvfd(fileno(SOCKET));

This unlikely to work on any system not purporting POSIX compliance. According to the author of the module, the socket passing code in Linux and FreeBSD is buggy, so use this module only when necessary.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • 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 exploiting the Monastery: (7)
    As of 2014-08-27 08:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (232 votes), past polls