Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes Linux's splice(2) work when splicing from a TCP socket?
    text
    copied!<p>I've been writing a little program for fun that transfers files over TCP in C on Linux. The program reads a file from a socket and writes it to file (or vice versa). I originally used read/write and the program worked correctly, but then I learned about <a href="http://manpages.courier-mta.org/htmlman2/splice.2.html" rel="noreferrer">splice</a> and wanted to give it a try. </p> <p>The code I wrote with splice works perfectly when reading from stdin (redirected file) and writing to the TCP socket, but fails immediately with splice setting errno to EINVAL when reading from socket and writing to stdout. The man page states that EINVAL is set when neither descriptor is a pipe (not the case), an offset is passed for a stream that can't seek (no offsets passed), or the filesystem doesn't support splicing, which leads me to my question: does this mean that TCP can splice <em>from</em> a pipe, but not <em>to</em>?</p> <p>I'm including the code below (minus error handling code) in the hopes that I've just done something wrong. It's based heavily on the <a href="http://en.wikipedia.org/wiki/Splice_(system_call)#Example" rel="noreferrer">Wikipedia example for splice</a>.</p> <pre><code>static void splice_all(int from, int to, long long bytes) { long long bytes_remaining; long result; bytes_remaining = bytes; while (bytes_remaining &gt; 0) { result = splice( from, NULL, to, NULL, bytes_remaining, SPLICE_F_MOVE | SPLICE_F_MORE ); if (result == -1) die("splice_all: splice"); bytes_remaining -= result; } } static void transfer(int from, int to, long long bytes) { int result; int pipes[2]; result = pipe(pipes); if (result == -1) die("transfer: pipe"); splice_all(from, pipes[1], bytes); splice_all(pipes[0], to, bytes); close(from); close(pipes[1]); close(pipes[0]); close(to); } </code></pre> <p>On a side note, I think that the above will block on the first <code>splice_all</code> when the file is large enough due to the pipe filling up(?), so I also have a version of the code that <code>fork</code>s to read and write from the pipe at the same time, but it has the same error as this version and is harder to read.</p> <p>EDIT: My kernel version is 2.6.22.18-co-0.7.3 (running coLinux on XP.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload