recvfrom() doesn't. - Unix

This is a discussion on recvfrom() doesn't. - Unix ; Hello, again. Is there something majorly wrong with this code? The writing process writes packets until the kernel runs out of buffer space and the process exits. The reader process never sees any data. /* writer.c */ #include #include #include ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: recvfrom() doesn't.

  1. recvfrom() doesn't.

    Hello, again.

    Is there something majorly wrong with this code? The writing process
    writes packets until the kernel runs out of buffer space and the
    process
    exits. The reader process never sees any data.

    /* writer.c */

    #include
    #include
    #include
    #include
    #include
    #include
    #include

    struct sockaddr_un su;

    int main(void)
    {
    long r;
    int fd;

    fd = socket(PF_UNIX, SOCK_DGRAM, 0);
    if (fd == -1) {
    perror("socket");
    return 1;
    }

    su.sun_family = PF_UNIX;
    memcpy(su.sun_path, "testsock", 8);

    if (bind(fd, (struct sockaddr *) &su, sizeof(su)) == -1) {
    perror("bind");
    return 1;
    }

    for (; {
    r = sendto(fd, "12345678", 8, 0, (struct sockaddr *) &su,
    sizeof(su));
    if (r == -1) {
    perror("sendto");
    unlink("testsock");
    return 1;
    }
    printf("info: wrote %ld\n", r);
    sleep(1);
    }

    return 0;
    }

    /* reader.c */

    #include
    #include
    #include
    #include
    #include
    #include
    #include

    struct sockaddr_un su;

    int main(void)
    {
    char buf[8];
    socklen_t len;
    long r;
    int fd;

    fd = socket(PF_UNIX, SOCK_DGRAM, 0);
    if (fd == -1) {
    perror("socket");
    return 1;
    }

    su.sun_family = PF_UNIX;
    memcpy(su.sun_path, "testsock", 8);

    for (; {
    r = recvfrom(fd, buf, 8, 0, (struct sockaddr *) &su, &len);
    if (r == -1) {
    perror("recvfrom");
    return 1;
    }
    printf("info: read %ld\n", r);
    sleep(1);
    }
    return 0;
    }

    I think I'm just about ready to give up with SOCK_DGRAM!

    MC


  2. Re: recvfrom() doesn't.

    On Jun 4, 5:39 am, artifact....@googlemail.com wrote:
    > Hello, again.
    >
    > Is there something majorly wrong with this code? The writing process
    > writes packets until the kernel runs out of buffer space and the
    > process
    > exits. The reader process never sees any data.
    >
    > /* writer.c */
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > struct sockaddr_un su;
    >
    > int main(void)
    > {
    > long r;
    > int fd;
    >
    > fd = socket(PF_UNIX, SOCK_DGRAM, 0);
    > if (fd == -1) {
    > perror("socket");
    > return 1;
    > }
    >
    > su.sun_family = PF_UNIX;
    > memcpy(su.sun_path, "testsock", 8);
    >
    > if (bind(fd, (struct sockaddr *) &su, sizeof(su)) == -1) {
    > perror("bind");
    > return 1;
    > }
    >
    > for (; {
    > r = sendto(fd, "12345678", 8, 0, (struct sockaddr *) &su,
    > sizeof(su));

    You want to send data to the socket youself created? Is it what you
    want?
    > if (r == -1) {
    > perror("sendto");
    > unlink("testsock");
    > return 1;
    > }
    > printf("info: wrote %ld\n", r);
    > sleep(1);
    > }
    >
    > return 0;
    >
    > }
    >
    > /* reader.c */
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > struct sockaddr_un su;
    >
    > int main(void)
    > {
    > char buf[8];
    > socklen_t len;
    > long r;
    > int fd;
    >
    > fd = socket(PF_UNIX, SOCK_DGRAM, 0);
    > if (fd == -1) {
    > perror("socket");
    > return 1;
    > }
    >
    > su.sun_family = PF_UNIX;
    > memcpy(su.sun_path, "testsock", 8);
    >
    > for (; {
    > r = recvfrom(fd, buf, 8, 0, (struct sockaddr *) &su, &len);

    Doing recvfrom() on a un-bind-ed socket cannot work.
    > if (r == -1) {
    > perror("recvfrom");
    > return 1;
    > }
    > printf("info: read %ld\n", r);
    > sleep(1);
    > }
    > return 0;
    >
    > }
    >
    > I think I'm just about ready to give up with SOCK_DGRAM!
    >
    > MC



  3. Re: recvfrom() doesn't.

    On Jun 4, 12:33 am, Bin Chen wrote:
    > On Jun 4, 5:39 am, artifact....@googlemail.com wrote:
    > >
    > > if (bind(fd, (struct sockaddr *) &su, sizeof(su)) == -1) {
    > > perror("bind");
    > > return 1;
    > > }

    >
    > > for (; {
    > > r = sendto(fd, "12345678", 8, 0, (struct sockaddr *) &su,
    > > sizeof(su));

    >
    > You want to send data to the socket youself created? Is it what you
    > want?


    The intention was that the writer process creates the socket
    in the filesystem with bind() and the reader process reads
    from it with recvfrom(). Is this not how PF_UNIX, SOCK_DGRAM sockets
    work?

    >
    > > if (r == -1) {
    > > perror("sendto");
    > > unlink("testsock");
    > > return 1;
    > > }
    > > printf("info: wrote %ld\n", r);
    > > sleep(1);
    > > }

    >
    > > return 0;

    >
    > > }

    >
    > > /* reader.c */

    >
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include

    >
    > > struct sockaddr_un su;

    >
    > > int main(void)
    > > {
    > > char buf[8];
    > > socklen_t len;
    > > long r;
    > > int fd;

    >
    > > fd = socket(PF_UNIX, SOCK_DGRAM, 0);
    > > if (fd == -1) {
    > > perror("socket");
    > > return 1;
    > > }

    >
    > > su.sun_family = PF_UNIX;
    > > memcpy(su.sun_path, "testsock", 8);

    >
    > > for (; {
    > > r = recvfrom(fd, buf, 8, 0, (struct sockaddr *) &su, &len);

    >
    > Doing recvfrom() on a un-bind-ed socket cannot work.


    So, the 'writer' process just calls sendto() on an un-binded
    socket and the 'reader' process is the one responsible for
    creating the socket with bind()? Perhaps I'm not
    understanding but this seems wrong somehow. I realise
    one process has to call bind() and given that I want the
    'writer' process to be the one in control of creating the
    socket, logically I assumed the 'writer' process had to
    call bind().

    MC


  4. Re: recvfrom() doesn't.

    On Jun 4, 8:15 am, artifact....@googlemail.com wrote:
    > On Jun 4, 12:33 am, Bin Chen wrote:
    >
    > > On Jun 4, 5:39 am, artifact....@googlemail.com wrote:

    >
    > > > if (bind(fd, (struct sockaddr *) &su, sizeof(su)) == -1) {
    > > > perror("bind");
    > > > return 1;
    > > > }

    >
    > > > for (; {
    > > > r = sendto(fd, "12345678", 8, 0, (struct sockaddr *) &su,
    > > > sizeof(su));

    >
    > > You want to send data to the socket youself created? Is it what you
    > > want?

    >
    > The intention was that the writer process creates the socket
    > in the filesystem with bind() and the reader process reads
    > from it with recvfrom(). Is this not how PF_UNIX, SOCK_DGRAM sockets
    > work?
    >
    >
    >
    >
    >
    > > > if (r == -1) {
    > > > perror("sendto");
    > > > unlink("testsock");
    > > > return 1;
    > > > }
    > > > printf("info: wrote %ld\n", r);
    > > > sleep(1);
    > > > }

    >
    > > > return 0;

    >
    > > > }

    >
    > > > /* reader.c */

    >
    > > > #include
    > > > #include
    > > > #include
    > > > #include
    > > > #include
    > > > #include
    > > > #include

    >
    > > > struct sockaddr_un su;

    >
    > > > int main(void)
    > > > {
    > > > char buf[8];
    > > > socklen_t len;
    > > > long r;
    > > > int fd;

    >
    > > > fd = socket(PF_UNIX, SOCK_DGRAM, 0);
    > > > if (fd == -1) {
    > > > perror("socket");
    > > > return 1;
    > > > }

    >
    > > > su.sun_family = PF_UNIX;
    > > > memcpy(su.sun_path, "testsock", 8);

    >
    > > > for (; {
    > > > r = recvfrom(fd, buf, 8, 0, (struct sockaddr *) &su, &len);

    >
    > > Doing recvfrom() on a un-bind-ed socket cannot work.

    >
    > So, the 'writer' process just calls sendto() on an un-binded
    > socket and the 'reader' process is the one responsible for
    > creating the socket with bind()? Perhaps I'm not
    > understanding but this seems wrong somehow. I realise
    > one process has to call bind() and given that I want the
    > 'writer' process to be the one in control of creating the
    > socket, logically I assumed the 'writer' process had to
    > call bind().


    Code will prove who is wrong, is the following code your desired?

    writer.c:
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    struct sockaddr_un su;

    int main(void)
    {
    long r;
    int fd;

    fd = socket(PF_UNIX, SOCK_DGRAM, 0);
    if (fd == -1) {
    perror("socket");
    return 1;
    }

    su.sun_family = PF_UNIX;
    memcpy(su.sun_path, "testsock", 8);

    for (; {
    r = sendto(fd, "12345678", 8, 0, (struct sockaddr *) &su,
    sizeof(su));
    if (r == -1) {
    perror("sendto");
    unlink("testsock");
    return 1;
    }
    printf("info: wrote %ld\n", r);
    sleep(1);
    }

    return 0;

    }

    reader.c:

    #include
    #include
    #include
    #include
    #include
    #include
    #include

    struct sockaddr_un su;

    int main(void)
    {
    char buf[8];
    socklen_t len;
    long r;
    int fd;

    fd = socket(PF_UNIX, SOCK_DGRAM, 0);
    if (fd == -1) {
    perror("socket");
    return 1;
    }

    su.sun_family = PF_UNIX;
    memcpy(su.sun_path, "testsock", 8);

    if (bind(fd, (struct sockaddr *) &su, sizeof(su)) == -1) {
    perror("bind");
    return 1;
    }

    for (; {
    r = recvfrom(fd, buf, 8, 0, (struct sockaddr *) &su, &len);
    if (r == -1) {
    perror("recvfrom");
    return 1;
    }
    printf("info: read %ld\n", r);
    sleep(1);
    }
    return 0;

    }





  5. Re: recvfrom() doesn't.

    On Jun 3, 2:39 pm, artifact....@googlemail.com wrote:

    > fd = socket(PF_UNIX, SOCK_DGRAM, 0);
    > if (fd == -1) {
    > perror("socket");
    > return 1;
    > }
    >
    > su.sun_family = PF_UNIX;
    > memcpy(su.sun_path, "testsock", 8);
    >
    > for (; {
    > r = recvfrom(fd, buf, 8, 0, (struct sockaddr *) &su, &len);


    You misunderstand what "recvfrom" does. It doesn't receive from the
    specified address. It receives from whatever and tells you what it
    received from. So setting 'su.sun_path' prior to calling 'recvfrom'
    does nothing. The socket you are trying to receive from doesn't go
    anywhere.

    DS


+ Reply to Thread