Networking Forums

Networking Forums > Computer Networking > Linux Networking > glibc: socket and fdopen

Reply
Thread Tools Display Modes

glibc: socket and fdopen

 
 
Thomas Jollans
Guest
Posts: n/a

 
      09-03-2004, 04:41 PM
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)

Thomas Jollans
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a

 
      09-03-2004, 04:57 PM
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)


This is off-topic for comp.lang.c; followups redirected. (It may be
off-topic in some of the remaining newsgroups as well; a shotgun
approach like this is seldom a good idea.)

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.

--
Keith Thompson (The_Other_Keith) kst-(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Thomas Jollans
Guest
Posts: n/a

 
      09-03-2004, 05:26 PM
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)

>
>
> This is off-topic for comp.lang.c; followups redirected. (It may be
> off-topic in some of the remaining newsgroups as well; a shotgun
> approach like this is seldom a good idea.)
>
> 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.

Thomas Jollans
 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a

 
      09-03-2004, 06:14 PM
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
 
Reply With Quote
 
Thomas Jollans
Guest
Posts: n/a

 
      09-03-2004, 06:53 PM
(E-Mail Removed) wrote:
> 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).

a-ha...
>
> 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...

Thanks. (Usually I do check the return values, but i kept this as short
as possible.)
> Regards, Jens

Thomas
 
Reply With Quote
 
Igmar Palsenberg
Guest
Posts: n/a

 
      09-03-2004, 07:19 PM
Thomas Jollans wrote:
> 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)


Because streams expect seekable objects.

> Thomas Jollans




Igmar
 
Reply With Quote
 
John Burton
Guest
Posts: n/a

 
      09-04-2004, 01:12 PM
Are you closing the socket properly? By using shutdown and then close?
Probably not your problem, but it could be

"Thomas Jollans" <(E-Mail Removed)> wrote in message
news:cha6si$m4j$03$(E-Mail Removed)...
>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)
>
> Thomas Jollans



 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a

 
      09-04-2004, 06:23 PM
John Burton wrote:
>
> Are you closing the socket properly? By using shutdown and then close?
> Probably not your problem, but it could be
>
> "Thomas Jollans" <(E-Mail Removed)> wrote in message
>
> >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)


You commit two errors in your reply. First, the reply is OT on
comp.lang.c, and should not be sent there. Second, you are
top-posting, which is frowned on in the comp.* hierarchy. Your
answer belongs after, or intermixed with, the quoted material
which has been snipped of anything not germane to your reply.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
James Antill
Guest
Posts: n/a

 
      09-05-2004, 12:21 AM
On Fri, 03 Sep 2004 19:26:57 +0200, Thomas Jollans 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)

>>
>>
>> This is off-topic for comp.lang.c; followups redirected. (It may be
>> off-topic in some of the remaining newsgroups as well; a shotgun
>> approach like this is seldom a good idea.)
>>
>> 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 );


Note that size should be the size of clientname, and should be of type
socklen_t.

>> FILE *clstream = fdopen ( newsock, "rw" );


Don't open rw, open two FILE * ... one read-only and one write-only. If
you don't do this you will lose data when you switch.

>> fputs ( "blah\n", clstream );
>>
>> fflush ( clstream );


You'd be better off changing the buffering method instead of calling
fflush() all the time.

>> int count = 20;
>> char line[count];


This is C99 specific, which you might not want to do.

>> fgets ( line, count, clstream );
>> printf ( "%s", line );
>> close ( newsock );


You need to use fclose(), which will close both the stream and the fd.

You also need to, at least, check the return values, for bind(),
listen(), accept(), fdopen(), and fflush().

--
James Antill -- (E-Mail Removed)
Need an efficient and powerful string library for C?
http://www.and.org/vstr/

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DNS glibc functions confused by trailing hyphen in host name davelasker@gmail.com Linux Networking 1 05-23-2008 08:22 PM
DNS glibc functions confused by trailing slash in host name davelasker@gmail.com Linux Networking 7 05-21-2008 07:53 PM
glibc, glibc sockets begin.middle.end@gmail.com Linux Networking 2 03-13-2008 03:23 AM
Test socket on Master Socket not working Kevin Cowans Broadband 14 11-15-2006 09:17 PM
WLAN Monitor 10038 Socket operation on non-socket Bill Windows Networking 1 03-01-2004 10:34 PM



1 2 3 4 5 6 7 8 9 10 11