lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] Can one find out how much data can be sent (lwip_write)


From: Indan Zupancic
Subject: Re: [lwip-users] Can one find out how much data can be sent (lwip_write) to a NON blocking socket without loss of data?
Date: Thu, 22 Dec 2022 11:23:29 +0100
User-agent: Roundcube Webmail/1.3.16

Hello Peter,

On 2022-12-22 00:20, Peter wrote:
Unfortunately the socket is non-blocking so if the data is
arriving too fast, some gets lost.

This is the most common mistake made with TCP socket programming:

Not handling short writes properly.

You also get this with blocking sockets, those also don't guarantee
to send all supplied data. In practice, you always get a short write
before the socket would block.

If you don't want short writes, use the RAW API.

The obvious solution (make the socket blocking) would cause
problems elsewhere.

Is there some way to get how many bytes a write socket can accept?

Check and handle the send return value correctly.

Doing some digging around, I have found stuff like this

//socket ready for writing
        if(FD_ISSET(new_sd, &write_flags)) {
            //printf("\nSocket ready for write");
            FD_CLR(new_sd, &write_flags);
            send(new_sd, out, 255, 0);
            memset(&out, 0, 255);
        }   //end if

but it doesn't seem to be suitable.

This solves your real problem: Knowing when to send more data.

(Assuming your send rate is not consistently too high for the
receiver to keep up, and your code does sends in bursts.)


Is there some way to get a buffer_space() function, so I can
extract only that many bytes (max) out of the UART buffer?

send() returns how many bytes are sent, you just have to keep the
remainder and send that when the socket can accept more (see above).

Greetings,

Indan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]