Using UNIX-Sockets for local interprocess communication

This is a discussion on Using UNIX-Sockets for local interprocess communication within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I am quite new to the world of Linux and have a little problem in understanding certain things about ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Using UNIX-Sockets for local interprocess communication

    Hi,

    I am quite new to the world of Linux and have a little problem in understanding certain things about UNIX-Sockets (AF_LOCAL/PF_LOCAL) that are stored in the file system (respectivly looking for confirmation of some points which I believe to have finally understood).

    I have two applications which runs on a local machine and are in a client-server relationship. The server bounds itself to the UNIX-Socket and waits for clients to connect it.

    Then something happens which I am not very familiar with. It seems like it that the server creates a file descriptor in the virtual user space (according to the man pages) and afterwards sends the client this address to let him know where to send data that a directed to the server. After this informatio exchange client and server are communication directly with each other and it seems like the unix-socket is only used for the initial connection build up.

    At least that is what I came up with trying to understand undocumented source code.
    So das the above mentioned any sense? I found very few information regarding the use of those sockets which are created in the virtual namespache for local interprocess communication. So I hope i can get somewhat of a conformation or a guidence if the stuff I wrote above is totally wrong.

    Here an example of the server where the address is set to the client:
    Code:
    struct sock_data {
      int fd;
      struct sockaddr_un sock;
    };
    
    static void send_sock(int fd, void *packet, int len, void *data)
    {
      struct sock_data *mine = data;
      int err;
      
      err = sendto(mine->fd, packet, len, 0, (struct sockaddr *) &mine->sock,
    	       sizeof(mine->sock));
      if(err != len){
        fprintf(stderr, "send_sock sending to fd %d ", mine->fd);
        perror("");
      }
    }
    The client receives this "address"-message and then write (so it seems) to the file descriptor the server has created:
    Code:
    sendto(fd, &data, data_len, 0, (struct sockaddr*) dst_addr, sizeof(struct sockaddr_un));

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    You're reading the code wrong. That structure is not being sent, it tells the sendto() function where to send the data, in this case, the data is in the "packet" buffer.

    This also should probably be under the Networking forum instead.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Oops, your are right. Lost myself in the code once more, sorry.

    What I am actually interested in is to get feedback whether this is a (usual) way how you can implement IPC. Using a UNIX-Socket as central point for the intitial connection build up and the creating on the server side a new fd in the virtual namespace nd sending the client this address to let him know where to send his messages?

    This is the part where the connected client is send the information where to send his data with "data_sun" being a struct sockaddr_un.
    Code:
    static void new_port_v1_v3(int fd, enum request_type type, 
    			   struct sockaddr_un *sock, int data_fd)
    {
      int n, err;
    
      switch(type){
      case REQ_NEW_CONTROL:
        err = setup_sock_port(fd, sock, data_fd);
        if(err) return;
        n = write(fd, &data_sun, sizeof(data_sun));
        if(n != sizeof(data_sun)){
          perror("Sending data socket name");
          close_descriptor(fd);
        }
        break;
      default:
        printf("Bad request type : %d\n", type);
        close_descriptor(fd);
      }
    }
    The client receives the wrote information/address? and sends his message by using this receivd address as destination
    Code:
    sendto(fd, &data, data_len, 0, (struct sockaddr*) dst_addr, sizeof(struct sockaddr_un));

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,678
    Not unless there's a good reason to. Is there a particular reason you want to do this?
    If the client and server are already connected, why not use that connection, instead of making a new one?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unix sockets
    By karas in forum Linux Programming
    Replies: 8
    Last Post: 10-13-2007, 01:20 AM
  2. Difference between Unix sockets and winsock.
    By antex in forum Networking/Device Communication
    Replies: 15
    Last Post: 01-21-2005, 04:53 PM
  3. Winsock vs Unix Sockets
    By khoxxxy in forum Networking/Device Communication
    Replies: 4
    Last Post: 08-05-2003, 06:13 AM
  4. Unix Sockets
    By prvindia in forum Linux Programming
    Replies: 5
    Last Post: 03-11-2003, 09:16 AM
  5. Interprocess Communication and UNIX???
    By atif in forum C Programming
    Replies: 3
    Last Post: 05-17-2002, 05:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21