[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Problem with socketpair , AF_UNIX and select call - can anybody through any light on this!



Howard Wilkinson wrote:
Can you suggest such a list - I can only find lists for POSIX threads!
comp.unix.programmer
Code goes as follows

    int fd[2] = { -1, -1 };
    fd_set rfd;
    int rv;
    char buf[1024];
    struct timeval timeout = { 10, 0 };

    rv = socketpair(PF_UNIX, SOCK_DGRAM, 0, fd);
    if (rv < 0) Err("socketpair");

    fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL, 0) | O_NONBLOCK);

    FD_ZERO(rfd);
    FD_SET(fd[0], rfd);

    rv = select(fd[0]+1, &rfd, NULL, NULL, &timeout);

    if (FD_ISSET(fd[0], rfd)) {
    	printf("Socket is ready to read\n");
    	rv = recv(fd[0], buf, sizeof(buf), MSG_DONTWAIT);
    	if (rv < 0) Err("recv - %s(%d)", strerror(errno), errno);
    	printf("Socket returned %d bytes\n", rv);
    } else {
    	printf("Socket is not ready to read\n");
    }
    exit (0);

I contend that this should (if I understand the semantics correctly) print out "Socket not ready to read" instead I get "recv - Resource temporarily unavailable(11)"
I haven't really looked at the code, except for making mods to get it to compile, but it works for me:

pika.localdomain:~/tmp 0:12> uname -a
Linux pika.localdomain 2.6.22.9-91.fc7 #1 SMP Thu Sep 27 20:47:39 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux

pika.localdomain:~/tmp 0:13> cat socketpair.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

int main()
{
   int fd[2] = { -1, -1 };
   fd_set rfd;
   int rv;
   char buf[1024];
   struct timeval timeout = { 10, 0 };
rv = socketpair(PF_UNIX, SOCK_DGRAM, 0, fd);
   if (rv < 0) printf("socketpair");
fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL, 0) | O_NONBLOCK); FD_ZERO(&rfd);
   FD_SET(fd[0], &rfd);
rv = select(fd[0]+1, &rfd, NULL, NULL, &timeout); if (FD_ISSET(fd[0], &rfd)) {
       printf("Socket is ready to read\n");
       rv = recv(fd[0], buf, sizeof(buf), MSG_DONTWAIT);
       if (rv < 0) printf("recv - %s(%d)", strerror(errno), errno);
       printf("Socket returned %d bytes\n", rv);
   } else {
       printf("Socket is not ready to read\n");
   }

   exit (0);
}

pika.localdomain:~/tmp 0:14> gcc -Wall socketpair.c

pika.localdomain:~/tmp 0:15> time ./a.out
Socket is not ready to read

real    0m10.000s
user    0m0.001s
sys     0m0.000s

pika.localdomain:~/tmp 0:16> ./a.out
Socket is not ready to read

--
Steve Croll


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]