Jump to content

[SOLVED] Socket Read timeout


photo

Recommended Posts

Hi, I´m developing one application that connects to a nodejs server using socket class. The send part works perfectly but the read part has a problem... I´m trying to read the stream every second, but when the stream is empty (the server has nothing to send me) , the read operation takes 5 seconds... (read timeout?) Is there any way to know if the stream has data? (In Qt sockets you have a signal that indicates data ready to read) Thanks, David

Link to comment

I tried to use Async class... 

 

Now I have another problem "async.run(socket,"gets")" command moves the latencies to the async class

 

 

I´m trying to read the stream every second, but when the stream is empty (the server has nothing to send me) , the read operation takes 5 seconds.

 

 I have a ids vector with all the running async instances and every second I check if async is not running to remove its id from the vector. If the server has nothing to send me the vector increases his size with 5 new ids before the first running instance finish.

 

The server only send data to the client when the client ask or when the database has changes.

 

This is the async example modificated.

 

Thanks,

 

     David

async_02.cpp

async_02.world

Link to comment

Have you tried Socket class in ASYNC mode ? By using this a read should return immediately even if no data is available (if this is what you need)

Link to comment

Have you tried Socket class in ASYNC mode ? By using this a read should return immediately even if no data is available (if this is what you need)

 

I think that is what i´m doing in the attached example.  Is something wrong in my code? Is there another way to do it?

 

Thanks,

 

    David

Link to comment

You are using new Async class, but most probably still with a socket in blocking mode. My proposal refered to setting the socket to non-blocking mode (= asynchronous mode). I would expect that then your read operations should return immediately (with Null/0 result in case no data send by server) even without requiring usage of Async class.

Link to comment

Non-blocking mode is the solution. Now, I put the non-block mode after the open and connect commands and works perfectly.

 

Thanks for your help.

 

     David

Link to comment
×
×
  • Create New...