[En-Nut-Discussion] TCP/IP socket writing while waiting for data toread?
Alexander Baranov
baranov at intech21.com
Wed Jun 29 16:36:15 CEST 2005
----- Original Message -----
From: "Sander Smeenk" <sandersmeenk at hotmail.com>
To: <en-nut-discussion at egnite.de>
Sent: Wednesday, June 29, 2005 8:19 AM
Subject: [En-Nut-Discussion] TCP/IP socket writing while waiting for data
toread?
> Hello heroes,
>
>
> I'm using a modified version of the 'tcps' application for my own
> application. Now, I have one thread that creates the socket, assigns a
> stream if the socket and stream are created succesfull AND if a client is
> connected, it sets a flag indicating that the connection has been made.
> After that, this thread waits for incoming data using fgets.
>
> In the mean time another thread is writing data to the stream (but only
when
> the client connected flag is set). But this doesn't seem to work well.
> Because it appears to me that it's not: memory leeks away, and finally the
> telnet client is being disconnected. Now, I was wondering, is it even
> possible to write data to a socket while another thread is waiting for
data
> on the same socket?
>
> Anyone got an answer to my question?
>
> Thanx in advance,
> Sander.
>
> PS: if it is not possible to both wait for data and write at 'the same
> time', it would be a good idea to create two streams right? One for
reading,
> one for writing. But the problem is that, in that case i would need 2
> terminals to 2 different tcp ports, and this is not really what I'd like.
>
> <code>
>
> typedef struct {
> FILE *stream;
> unsigned int iClientConnected;
> } T_TelnetChannel;
>
>
>
> Global:
> volatile T_TelnetChannel TC_Channel1;
>
>
> In telnet start thread:
> extern volatile T_TelnetChannel TC_Channel1;
>
> if ((sock = NutTcpCreateSocket()) != 0) {
> ..
> if (NutTcpAccept(sock, TELNET_PORTNUM) == 0) {
> ..
> if ((TC_Channel1.stream = _fdopen((int) sock, "r+b")) !=
0)
> {
> ..
> fgets(cFrame, 3300, stream)
> }
> }
> }
>
> And while the telnet thread waits for incoming data, in another thread, i
> do:
>
> The 'write thread':
> extern volatile T_TelnetChannel TC_Channel1;
>
> fputs(cFrame, TC_Channel1.stream);
>
>
> _______________________________________________
> En-Nut-Discussion mailing list
> En-Nut-Discussion at egnite.de
> http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
>
>
I just want to note that instead of switching flags I check:
name_of_my_socket->so_state == TCPS_ESTABLISHED
Regards, Alexander
More information about the En-Nut-Discussion
mailing list