In comp.programming Thomas Jollans <(E-Mail Removed)> wrote:
> Keith Thompson wrote:
>> Thomas Jollans <(E-Mail Removed)> writes:
>>
>>>I am playing around with sockets and have the following problem.
>>>I have the socket newsock created by accept(). This code:
>>>
>>>FILE *clstream = fdopen ( newsock, "rw" );
>>>fputs ( "message\n", clstream );
>>>fflush ( clstream );
>>>
>>>does NOT send anything to the telnet client at the other end. Why?
>>>(gcc 3.3.3, glibc 2.3.3)
>>
>> BTW, when I compile the above lines I get multiple syntax errors. If
>> you provide a small complete program that exhibits the problem, you're
>> more likely to get answers.
> OK. here's an example:
>> #include <stdlib.h>
>> #include <unistd.h>
>> #include <sys/types.h>
>> #include <stdio.h>
>> #include <sys/socket.h>
>> #include <netinet/in.h>
>>
>> int main (int argc, char **argv)
>> {
>> int sock;
>> struct sockaddr_in name;
>>
>> sock = socket (PF_INET, SOCK_STREAM, 0);
>>
>> name.sin_family = AF_INET;
>> name.sin_port = htons (12345);
>> name.sin_addr.s_addr = htonl (INADDR_ANY);
>> bind (sock, (struct sockaddr *) &name, sizeof (name));
>>
>> listen (sock, 1);
>>
>> struct sockaddr_in clientname;
>> size_t size;
>> int newsock = accept ( sock, (struct sockaddr *) &clientname, &size );
>>
>> FILE *clstream = fdopen ( newsock, "rw" );
>>
>> fputs ( "blah\n", clstream );
>>
>> fflush ( clstream );
>>
>> int count = 20;
>> char line[count];
>>
>> fgets ( line, count, clstream );
>> printf ( "%s", line );
>> close ( newsock );
>> }
> >
> On my system it compiles perfectly with gcc test.c -o test, but the
> -ansi flag gives the following warning:
>> test.c:26: warning: initialization makes pointer from integer without a cast
> I don't understand the warning, either.
fdopen() isn't a standard C function, so it won't get declared in
<stdio.h> if you compile with '-ansi' - and thus the compiler has
to assume that fdopen() returns an int. (BTW, "normal" C, i.e.
C89, does not allow the definition of new variables after the
first executable standard, so your code will probably not compile
with a compiler that doesn't support that particular subset of C99).
But the main problem is the flags you use in fdopen(), "rw" is
meaningless, use "r+" instead. And use fclose() on clstream,
not close() on newsock. And you should always check the return
values of the functions you use...
Regards, Jens
--
\ Jens Thoms Toerring ___
(E-Mail Removed)
\__________________________
http://www.toerring.de