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

--
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list



Relevant Pages

  • socket programming, very low connection performance.
    ... This is just a test for socket connection performance. ... const int SVR_PORT = 10000; ... char *get_time_str{ ... struct sockaddr_in svr_addr; ...
    (comp.unix.programmer)
  • Re: How can I send/receive an integer via socket call in c?
    ... I would like to send and receive an integer via a socket calls in c: ... char line; ... // how to covert what i read to an int? ... have an appropriate newsgroups line in your header for your mail to be seen, ...
    (comp.lang.c.moderated)
  • unix domain sockets
    ... some reason when i #define NAME "my_sock" it doesn't create the socket ... so in order for the server to run multiple times I need to do ... int orig_sock; ... void clean_up(int, char *); ...
    (comp.unix.programmer)
  • Re: Multiple writes to client socket
    ... is it a TCP or UDP socket? ... int fipcp_start_server{int sock; ... int i; struct sockaddr_in clientname; size_t size; char *method_name; ...
    (comp.unix.programmer)
  • Re: Basic stuff, how to retrieve a web page using C Socket???
    ... int main{ ... struct sockaddr_in serveraddr; ... char servername; ... //now create socket SOP ...
    (comp.programming)