unix socket at failure ?

AMa111 1 Reputation point
2022-01-12T08:18:09.293+00:00

when I call connect() with unix socket (streaming) it creates socket file if connect fails (due no listener).
should it be that only listener creates sock file and connect() just opens it ?

Windows development Windows API - Win32
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    2022-01-12T09:18:10.5+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The socket function creates a socket that is bound to a specific transport service provider.
    The listen function places a socket in a state in which it is listening for an incoming connection.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. AMa111 1 Reputation point
    2022-01-12T11:56:40.593+00:00

    perhaps you missunderstood my question, connect() call below makes foobar.sock when there is no listener/server.

    pseudo code below:

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    server = gethostbyname("foobar.sock");

    if (server == NULL) {
    fprintf(stderr,"ERROR, no such host\n");
    exit(0);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
    serv_addr.sin_port = htons(portno);

    /* Now connect to the server /
    if (connect(sockfd, (struct sockaddr
    )&serv_addr, sizeof(serv_addr)) < 0) {
    perror("ERROR connecting");
    exit(1);
    }

    BTW: links you included do not even mention unix domain sockets.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.