Your code sample is much neater than mine. BTW: this is the best mailing list I've ever seen. I can get helpful response almost in real-time. Thanks everyone!
On Mon, Jun 20, 2011 at 2:54 PM, Pierre-Yves Kerembellec < [email protected]> wrote: > Thanks Pierre-Yves! > I modified my code setting non-blocking socket using the way you described. > The problem is still the same. > > > You need to listen(fd, backlog) on your server socket before attempting to > connect. How about something like this: > > #include <stdio.h> > #include <unistd.h> > #include <netdb.h> > #include <sys/ioctl.h> > > int server_socket(char *address, char *port) > { > struct addrinfo *info = NULL; > int handle = -1, flag = 1; > > if (!getaddrinfo(address, port, NULL, &info) && > (handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) >= 0 && > !setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) > && > !bind(handle, (struct sockaddr *)info->ai_addr, sizeof(struct > sockaddr)) && > !listen(handle, -1) && > !ioctl(handle, FIONBIO, &flag)) > { > freeaddrinfo(info); > return handle; > } > freeaddrinfo(info); > close(handle); > return -1; > > } > > int main(int argc, char **argv) > { > int fd; > > if ((fd = server_socket("localhost", "http-alt")) < 0) > { > perror("server_socket"); > return 1; > } > // do something like accept() ... > return 0; > } > > -- > Pierre-Yves > >
_______________________________________________ libev mailing list [email protected] http://lists.schmorp.de/cgi-bin/mailman/listinfo/libev
