Subject: fdopen() and winsock
From: Bob Crispen <[email-masked]>
To: [email-masked]
Date: Wed, 18 Aug 1999 22:32:48 -0500

This has come up several times in the archives, but the only
reply I've seen so far is "you can't do that" so I thought
I'd try again, just in case.

When I compile with Cygwin 2.95 and don't set the -mno-cygwin
flag, then the following works just fine:

    /*
    * Convert the socket to a stream
    */
    if (0 > (sf = fdopen(s, "r+"))) {
	fprintf(stderr, "fdopen failed: %s\n", strerror(errno));
	close(s);
	exit (EXIT_FAILURE);
    }

    /*
    * Get news server acknowledgement of logon
    */
    bzero(reply, sizeof(reply));
    if (2 > (fscanf(sf, "%d %[^\r\n]", &status, reply))) {
	fprintf(stderr, "Logon ack read failed: %s\n", strerror(errno));
	close(s);
	exit (EXIT_FAILURE);
    }

[I defined bzero as a macro.  I just got lazy and didn't want
to change it everywhere in the code.]

When I compile with -mno-cygwin and use winsock.h and -lwsock32,
(and use WSAStartup to start up winsock) then the fscanf fails
with a "bad file descriptor" error, even though the file descriptor
inside the FILE is in fact the same value as the socket.  The only
thing that works is

    bzero(reply, sizeof(reply));
    bzero(sockbuf, sizeof(sockbuf));
    if (recv(s, sockbuf, sizeof(sockbuf), 0) < 0) {
	fprintf(stderr, "Logon ack read failed: %s\n", strerror(errno));
	close(s);
	exit (EXIT_FAILURE);
    }
    if (2 > (sscanf(sockbuf, "%d %[^\r\n]", &status, reply))) {
	fprintf(stderr, "Logon ack convert failed: %s\n", strerror(errno));
	close(s);
	exit (EXIT_FAILURE);
    }

Has anyone solved the problem of using fdopen to convert a socket
to a read-write stream?
--
Bob Crispen
crispen at hiwaay dot net
What we're looking for: destinations.
What we end up getting: journeys.

--
Want to unsubscribe from this list?
Send a message to [email-masked]