[En-Nut-Discussion] TCP/IP socket writing while waiting for data to read?

Sander Smeenk sandersmeenk at hotmail.com
Wed Jun 29 14:19:45 CEST 2005


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);





More information about the En-Nut-Discussion mailing list