Search code examples
csocketsunix-socket

send() on a UDS socket returns without sending the entire data. Why?


I am working on an embedded device running Linux. In my application, a server thread opens a UDS socket and waits for connections. When a client (a different application/process) connects, it processes the request and sends the requested data.

The requested data is usually more than 52KB. The send() call, however, returns after sending only 32064 bytes. This behavior is seen intermittently; often the entire 52KB or more is sent to the client process without a problem.

Could someone please suggest what are the possibile reasons for send() to return in the middle of the transfer?

I don't have the client code, just the server code, but I'm not allowed to post it here. From what I have, I don't see the server code ever closing the said socket. It is only closed when the entire application is shutting down.

Is there a way the socket connection could end without either the client or server explicitly closing it? As I don't have the client clode, I'm looking for possibilities that the server code is doing something stupid.

Appreciate any inputs on this.

Thanks!


Solution

  • On success send() return the number of bytes sent, this does not guarantee that all your data has been written, you need call it again with remain data, this behaviour depends of internals buffers sizes. The same behaviour is for the read() function.

    "The only difference between send() and write(2) is the presence of flags. With a zero flags argument, send() is equivalent to write(2)." Source: man 2 send

    "The number of bytes written may be less than count if, for example, there is insufficient space on the underlying physical medium, or the RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)), or the call was interrupted by a signal handler after having written less than count bytes. (See also pipe(7).)" Source: man 2 write