Re: send to socket: Bad file descriptor

From: Kasper Dupont (kasperd_at_daimi.au.dk)
Date: 04/27/04


Date: Tue, 27 Apr 2004 17:04:35 +0200

ormancey wrote:
>
> hi everybody,
>
> a newbie question again
>
> i made a socket in a task
> and i read data on this socket in a child task
> i want to write data to this socket with a other child but i have this message
> send: Bad file descriptor
>
> if some one has an idea to let me write to this socket ??

Sounds like you created the child process before creating
the socket. That means the two processes will not share
file descriptors, so accessing the same file descriptor
number in the child will give a different file, or in
your case probably an unused file descriptor.

Three possible solutions:
1) Create the socket before forking
2) Share the array of filedescriptors by the use of
   clone with appropriate flags
3) Pass the file descriptor over a UNIX domain socket.
   (AFAIK you can do something like this, but I never
   tried it myself).

-- 
Kasper Dupont -- der bruger for meget tid paa usenet.
For sending spam use abuse@mk.lir.dk and kasperd@mk.lir.dk
/* Would you like fries with that? */


Relevant Pages

  • Porting Unix sockets code: socket connection to a child
    ... I'm porting a Unix app which creates a child process, ... to the new process via a socket [this is a client/server application, ... the client talks to the child via a socket]. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: given an fd, how to tell if its open?
    ... Your process may inherit open fd's from a parent ... like a socket. ... start any new Applix sessions - disastrous with over 400 users over ... up on a consistent file descriptor. ...
    (comp.unix.programmer)
  • Re: send file descriptor via ipc
    ... numbers into 'struct file' references. ... buffer for the remote socket. ... them into the file descriptor array of the receiving process. ... descriptors that are referenced only by disconnected UNIX domain sockets ...
    (freebsd-hackers)
  • Re: Proposal: a revoke() system call
    ... You could achieve something of the same end by opening /dev/null and then dup2'ing to the file descriptor you want to revoke, ... Right now there's a known issue that calling closeon a socket from one thread doesn't interrupt a socket in a blocking I/O call from another thread -- you first have to call shutdown, ... Another example of a "reader thread" would be ... the main thread of a daemon that accepts the incoming connections ...
    (freebsd-arch)
  • Re: How to find a certain socket?
    ... On Wed, 20 Sep 2006, Taras Danko wrote: ... implementation iterates over the "allproc" list and checks every process' file descriptors list to find needed socket. ... The semantics of closing sockets open in processes are a bit tricky -- first off, you really don't want to actually close the file descriptor, as the application may misbehave in rather unfortunate ways, such as writing data to the wrong files, etc. So in that sense, what you're doing is better than actually closing the file descriptor, which would cause that to happen. ...
    (freebsd-hackers)