0

I'm trying to understand how local sockets (Unix domain socket) works, specifically how to use them with QT. It is my understanding that Unix domain socket is always reliable and no data is lost. Looking at these examples these are the steps (making it simple), considering a Server (producer) and a Client (consumer)..

  1. A qLocalServer (on the Server) create a socket, bind it to a known location and listen for incoming connections..
  2. A qLocalSocket (on the client) connect to the known location. On the server connection is accepted.
  3. Server can send data to the socket using write method (of the qLocalSocket instance returned by qLocalServer->nextPendingConnection())

When the socket buffer is full, any write by the server is blocked as soon as the client read from the socket and free the socket buffer. This is the part I don't understand: suppose the socket buffer is full and the server keeps writing .. where all these data are stored and how to control the data structure holding these data? Is there any way to discard these data? Imagine a server that produce data 20 times/second and a client that consume 1 time/second, I want to drop all data in bewtween (better real time data than ALL the data in my use case).

rok
  • 2,574
  • 3
  • 23
  • 44
  • You have to read out the data and discard it by your own. – chehrlic Jan 19 '22 at 16:51
  • If the client does not read the data fast enough then this is bad design. The easiest way is to change the client. Otherwise the server might need another software layer on top of the socket that discards new data as long as the socket buffer is full. The server cannot remove old data from the socket. – Bodo Jan 19 '22 at 17:01
  • If I've read this correctly you're saying that your server generates updates far quicker than the client can process them. That being the case it might be better for the server to wait for the client to request the latest updates as/when it's ready. – G.M. Jan 19 '22 at 17:03

1 Answers1

0

There's nothing unique about the Qt socket classes (for the purposes of this question).

When the socket buffer is full, any write by the server is blocked as soon as the client read from the socket and free the socket buffer.

No - the write is blocked until the client performs a read, freeing up some of the buffer. (Perhaps this is what you meant and we just have a minor language barrier.)

Once the server writes to the socket, the data can't be "un-written" so how best to handle this depends on your application, and whether you have more control over the programming of the server or the client.

mzimmers
  • 857
  • 7
  • 17
  • Yes, that was what I meant (until..). What I don't understand is how the write is blocked. I mean, I tried to run the linked example with minor modification to make the client never read and the server continuosly writing. At some point the socket is full, but server continue (doesn't) get stuck.. this means `write` is not a blocking function.. but actually these data are not written in the socket, but where ? Of course I observed a memory leak, so somewhere this data are stored and somehow when the socket is free they are pushed in the socket.. – rok Jan 21 '22 at 11:16
  • Are you checking the return value from your write? If it's (-1), you can call QIODevice::errorString() to get a human-readable description of the last error. I'd be interested in seeing this. – mzimmers Jan 21 '22 at 23:35
  • Wait a minute: in my original answer, I made some assumptions that I now see aren't necessarily true. Which exact Qt class are you using for your socket, and which method for the write? – mzimmers Jan 21 '22 at 23:44