Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2010

    problems with unix named socket. How to send command outputs remotely

    I am having some problems with an AF_UNIX named socket.
    I have programmed a server that accepts the conexion of a client to manage the system remotely. In some cases, the client sends an order to execute some standar commands in the server and I want the server to send back to the client the outputs of that commands.

    I am not sure if there is a better way to do it, but this is what I did:

    1. The server creates a local named socket (/tmp/named_socket) when it starts and two inet sockets (one to comunicate with the client and another one to send the output of the command executed. The client connects to both sockets and prints all that it receives in this "command socket" to an output window) <- I could use the same socket but then I would have to parse the information in order to know what is the output of a command and what is not, so I think it is easier in this way.

    2. In the server, one thread is dedicated to read the local named socket and sends to the client all it reads through the "command socket".

    3. I execute the commands redirecting the output to another program I wrote (called "named_redirect") that takes what it receives from the stdin and writes it to the named socket. For example: if I execute 'ls /home | named_redirect" the output of the ls command is written to the /tmp/named_socket and then, the server thread will read it and send it to the client

    This all works fine, but sometimes (i don't know why) the named_redirect program stops working and the client stops receiving command outputs. It stops working sometimes after one day sometimes after one week.. and I don't know how to force the error so it is very dificult to debug

    I show here some of the code, if you see something wrong or think there is a better way to do it please tell me!

    ++++++ named_redirect code +++++++++++++++++++++++
    #define NAMED_SOCKET    "/tmp/named_socket"
    int main(int argc, char * argv[]){
       struct sockaddr_un name;
       size_t size;
       char cadena[1024];
       int n;
       /* Create the socket. */
       sock = socket(AF_UNIX, SOCK_DGRAM, 0); 
       if (sock < 0){ 
          perror ("socket");
       name.sun_family = AF_UNIX;
       strncpy(name.sun_path, NAMED_SOCKET, sizeof(name.sun_path));
       size = (offsetof(struct sockaddr_un, sun_path) + strlen(name.sun_path) + 1); 
          // read stdin
          n = read(0, cadena, sizeof(cadena));
          if(n < 0){ 
             perror("error al leer stdin");
          }else if(n > 0){
             // send to named socket
             if(sendto(sock, cadena, strlen(cadena), 0, (struct sockaddr *)&name, sizeof(struct sockaddr_un)) < 0){ 
                perror("error en write");
       return 0;
    ++++++++++++++++ in server code ++++++++++++++++++++++++++
    /////////// create the named socket at startup ///////////////////////
       /* Create the socket. */
       sock = socket(PF_UNIX, SOCK_DGRAM, 0);
       if (sock < 0){
          perror ("socket local");
          return -1;
       /* Bind a name to the socket. */
       name.sun_family = AF_FILE;
       strncpy (name.sun_path, filename, sizeof(name.sun_path));
       /* The size of the address is
          the offset of the start of the filename,
          plus its length,
          plus one for the terminating null byte. */
       size = (offsetof(struct sockaddr_un, sun_path) + strlen(name.sun_path) + 1);
       if(bind(sock, (struct sockaddr *) &name, size) < 0)
          perror ("bindeo socket local");
          return -1;
    // (somewhere in the code a dedicated thread keeps reading the named socket and sending the data to the client)
    /////// close named socket before exit ///////////////
    any help or suggestion will be apreciate!


  2. #2
    Why not use ssh?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts