Thread: Sockets - The Error That Shouldn't Be There

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Montreal, Quebec

    Question Sockets - The Error That Shouldn't Be There

    Ok, I've been learning out of this book, "Advanced Linux Programming," created by Code Sourcery. Now, it gives an example of creating, using and destroying sockets. I copied the source code completely, I even tried downloading the source code off of their website. But, I get these errors:

    socket-server.cpp: In function `int main(int, char* const*)':
    socket-server.cpp:75: cannot convert `sockaddr_un*' to `const sockaddr*' for
       argument `2' to `int bind(int, const sockaddr*, unsigned int)'
    socket-server.cpp:97: cannot convert `sockaddr_un' to `sockaddr*' for argument
       `2' to `int accept(int, sockaddr*, socklen_t*)'

    And here's the source code:

    //Server of socket test
    #include <iostream.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    //  Read text from the socket and print it out.
    // Continue until the socket closes. Return 
    // nonzero if the client sent a "quit" message, 
    // zero otherwise.
    int server(int client_socket)
          int length;
          char* text;
          //  First, read the length of the text message
          // from the socket. If read returns zero, the 
          // client closed the connection.
          if(read(client_socket, &length, sizeof(length))==0)
    	  return 0;
          // Allocate a buffer to hold the text.
          // Read the text itself, and print it.
          read(client_socket, text, length);
          cout << text << endl;
          // Free the buffer.
          // If the client sent the message "quit," we're all done.
    	  return 1;
    int main(int argc, char* const argv[])
      const char* const socket_name=argv[1];
      int socket_fd;
      struct sockaddr_un name;
      int client_sent_quit_message;
      // Create the socket.
      socket_fd=socket(PF_LOCAL, SOCK_STREAM, 0);
      // Indicate that this is a server.
      strcpy(name.sun_path, socket_name);
      bind(socket_fd,&name, SUN_LEN(&name));
      // Listen for connections.
      listen(socket_fd, 5);
      // Repeatedly accept connections, 
      // spinning off one server() to deal with each
      // client. Continue until a client sends a "quit"
      // message.
          struct sockaddr_un client_name;
          socklen_t client_name_len;
          int client_socket_fd;
          // Accept a connection.
          client_socket_fd=accept(socket_fd, client_name, &client_name_len);
          // Handle the connection.
          // Close our end of the connection.
      // Remove the socket file.
      return 0;

  2. #2
    Join Date
    Sep 2003
    Maybe you don't use the same Linux that they do. bind and accept take pointers to sockaddr, and my current system doesn't even recognize sockaddr_un or have it in any of the socket headers. Which leads me to believe that it's an extension...or a typo.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 01:43 PM
  2. Cross platform sockets
    By zacs7 in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-27-2007, 05:16 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM