Thread: Problem with socket descriptors

  1. #1
    Registered User
    Join Date
    Jul 2009

    Problem with socket descriptors

    Hi to all. I'm programming a simple client/server application with the Berkeley API socket(), bind(), listen() and accept(). For now, i'm using a TCP connection to realize it.
    The server process works with fork() to manage the different client connections, creating a child process for every connecting client.
    Then, my problem is about the possibility for a child process to write on a socket that is not the socket open with "its" client. Let see an example:
    Father process P.
    Client A connects with P.
    P forks and creates a child process 1 and opens socket 1A.
    Client B connects with P.
    P forks and creates a child process 2 and opens socket 2B.

    Then, i would like to have process 1 writing on socket 2B.
    The problem is that the socket descriptor returned by the accept() is always the same for every connecting client. I thought to store the socket descriptors and to use them for writing on the sockets from different processes, passing the current socket descriptonr as argument in write(). But this is impossible just because the accept() returns the same socket descriptor. How can i fix this?
    Thank you

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Quote Originally Posted by McKracken View Post
    Then, i would like to have process 1 writing on socket 2B.
    You can't.

    But this is impossible
    Correct. Even if you passed the descriptor back -- which you would have to use a socket from the parent to the child to do that, since otherwise they do not communicate -- 2B will not be open for P.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 04:46 AM
  3. Problem with network code
    By cornholio in forum Linux Programming
    Replies: 1
    Last Post: 12-20-2005, 01:21 AM
  4. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 07:01 AM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 10:01 AM