CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  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

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