CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2008
    Posts
    3

    Problem with bind

    Here's my function

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdarg.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <ctype.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <errno.h>
    #include <signal.h>

    #define SERVER_PATH "/tmp/srv"
    #define BUFFER_LENGTH 250

    static int open_sock(void) {

    int sd=-1;
    int r=-1;
    struct sockaddr_un serv_addr;

    if(sd = socket(AF_UNIX, SOCK_STREAM, 0) < 0) {

    perror("socket() failed");
    return sd;
    }

    memset(&serv_addr,0,sizeof(serv_addr));
    serv_addr.sun_family = AF_UNIX;
    strcpy(serv_addr.sun_path, SERVER_PATH);

    if(bind(sd, (struct sockaddr*)&serv_addr, sizeof(&serv_addr)) < 0) {
    perror("bind() failed");
    return -1;
    }

    chmod (serv_addr.sun_path,0777);

    listen(sd, 20);
    return sd;
    }

    Basically, I am trying to open a Unix Socket for listening request. However, when I execute the file, the bind() always return an error with
    "bind() failed: Socket operation on non-socket"

    Can anyone tell me what might cause this issue in bind? Thanks
    The socket 'sd' value before bind is 0, which I find kind of strange.
    Also, the path '/tmp/srv' is non-existence before socket creation. Should I make this path first?
    Last edited by highlord101; August 19th, 2008 at 06:26 AM. Reason: Adding information

  2. #2
    Join Date
    Aug 2007
    Location
    Birmingham, UK
    Posts
    360

    Re: Problem with bind

    What happens if you use AF_INET to opend the socket rather than AF_UNIX?

  3. #3
    Join Date
    Aug 2008
    Posts
    3

    Re: Problem with bind

    Unfortunately, the AF_INET fails as well. I am thinking could it be the fact that there is something in the Linux setting itself that disallow the creation of socket. I'll keep searching. Thanks

  4. #4
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Problem with bind

    I think you have a problem with the socket creation and the error checking:
    [code]
    if( (sd = socket(AF_UNIX, SOCK_STREAM, 0) ) < 0) {
    [code]

    What you are doing is checking if socket() < 0 and then assigning the value of this comparison to sd. The additional parenthesis in the above code should do the trick.

    HTH,
    Richard

  5. #5
    Join Date
    Aug 2008
    Posts
    3

    Re: Problem with bind

    Yes, it does. Thanks bro..
    How careless of me.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured