Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Search this Thread
Old 10-25-2007, 11:39 AM   #1
Registered: Oct 2003
Posts: 42

Rep: Reputation: 19
Splice syscall does not work

I'm trying to do a splice syscall from
tcp socket to a write side of a pipe
and getting BAD FILE DESCRIPTOR error...

Is it expected behaviour?
Old 10-25-2007, 01:45 PM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Can you show what code is causing this error? Please post code in [code] tags to preserve formatting ans aid readability.

Last edited by matthewg42; 12-15-2007 at 07:19 PM.
Old 10-26-2007, 06:21 AM   #3
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 19
Ok, here is the code it is run with:
Btw, where one can find the full doc for splice syscall?


// tsplice.c - test splice syscall 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

sa_pton(int af, const char *cp, struct sockaddr *sa)
  sa->sa_family = af;
  return inet_pton(af, cp, &((struct sockaddr_in *) sa)->sin_addr);


sa_set_port(struct sockaddr *sa, int port)
  ((struct sockaddr_in *) sa)->sin_port = htons(port);

sa_ptonp(int af, const char *cp, struct sockaddr *sa)
  char *ports;
  int r;
  char *cp2 = alloca(strlen(cp) + 1);

  strcpy(cp2, cp);
  ports = strrchr(cp2, ':');
  if (ports)
    *ports = 0;

  sa->sa_family = af;
  r =  inet_pton(af, cp2, &((struct sockaddr_in *) sa)->sin_addr);
  if (r > 0)
    sa_set_port(sa, atoi(ports+1));

  return r;


void error(const char *m)

char data[] = "012345678";

#define DATASIZE (sizeof(data))

char  rbuf[256];

main(int argc, char *argv[])
  struct sockaddr sa;
  struct sockaddr lsa;
  int s1, s2, s3;
  int pipes[2];
  int r1, r2, r3;
  loff_t o1 = 0, o2 = 0;
  int alen = sizeof(lsa);

  sa_ptonp(AF_INET, argv[1], &sa);

  s1 = socket(AF_INET, SOCK_STREAM, 0);
  if (s1 < 0)

  r1 = bind(s1, &sa, sizeof(sa));
  if (r1 < 0)

  r1 = listen(s1, 1);
  if (r1 < 0)

  s2 = socket(AF_INET, SOCK_STREAM, 0);
  if (s2 < 0)
    error("second socket");

  r1 = connect(s2, &sa, sizeof(sa));
  if (r1 < 0)

  s3 = accept(s1, &lsa, &alen);
  if (s3 < 0)

  r1 = write(s2, data, DATASIZE);
  r2 = splice(s3, &o1, pipes[1], &o2, DATASIZE, 0);
  if (r2 < 0)
      error("splice syscall");

  printf("r1=%d, r2=%d\n", r1, r2);

  r3 = read(pipes[0], rbuf, DATASIZE);

  printf("r1=%d, r2=%d, r3=%d, data=%s\n", r1, r2, r3, rbuf);



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
syscall hook robertos Linux - Kernel 15 03-09-2007 08:33 PM
need help with splice() and disk read graphicsMan Programming 0 02-19-2007 12:13 PM
DISCUSSION: Splice Traffic with Perlbal jeremy LinuxAnswers Discussion 0 02-05-2007 06:38 PM
Deleting elements from array in perl with splice signalno9 Programming 2 08-16-2005 11:57 PM
how do i splice an x-box controller cable? ubuntu-addict General 4 11-12-2004 08:22 AM

All times are GMT -5. The time now is 08:29 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration