Like mentioned in the Subject, I'm wondering what is your package or lib of choice when you need to use non-blocking TCP sockets.
For Raw TCP I use the raw sockets api. There is no need to use any big chunky external library for something that already provides a well designed cross plattform API such as berkley sockets.
It's easy as that. The only thing that is slightly more complex is the DataAvailable function because it uses select and select is complicated (the easier poll is not provided by the fpcs socket or baseunix unit and therefore I had to revert back to select, but poll is usually easier):
Ah you are right, I confused this, it was the windows version that was missing (WSAPoll in winsock2 unit). That said, it can probably be easiely defined as external function, but in my wrapper lib I was just lazy and used what was there (select)It's easy as that. The only thing that is slightly more complex is the DataAvailable function because it uses select and select is complicated (the easier poll is not provided by the fpcs socket or baseunix unit and therefore I had to revert back to select, but poll is usually easier):
https://www.freepascal.org/docs-html/rtl/baseunix/fppoll.html
Here is the algorithm for the example I want to produce:
- Start the thread
- Send the greeting
- Send the first ping, reset 5s timer
- Infinite loop:
- Is there any data to read? Receive data and display it
- Has the 5s timer dinged? Send Ping, reset 5s timer
In my opinion, informed by some theory books on sockets, it makes sense that I use a non blocking socket for this algorithm.
What do you guys say?
Is this doable with Indy, like Marco answered on Reply #1?
Do I need to go full low level and use something else not provided by the abstractions that Synapse/Indy/LNet facilitate?
Stop right there. When you say "ping", do you literally mean ICMP echo request?
And before we go much further, are you restricting consideration to TCP or are you interested in UDP?
...
Assuming that your ping is a TCP message ping and not a literal ICMP ping, this can be done with poll/select. Using the SimpleSockets wrapper by me to keep it shorter, but can also be easiely done using raw sockets:
...
I understood what you were asking. I'll explain in a bit more detail.
Yeah, you're absolutely right and I made a mess of it again. It's not ICMP related. I'm sorry for the confusion that I spread!!
You're then gonna argue that the triple handshake of a TCP connection and the methods that have been put in it's planning are enough to make the connection be kept alive, and you're right.
My example is based on a custom protocol that relies on New Line(Indy's WriteLn on a socket) terminated strings that I did not invent and I'm kinda stuck with.
Test message\r\n
Test m
essage\r\n
Yeah, you're absolutely right and I made a mess of it again. It's not ICMP related. I'm sorry for the confusion that I spread!!
No apology needed. But if it were ICMP you'd (a) need to use a different API and (b) find that on unix ping and tracert are either setuid root or have a special POSIX attribute to allow them to be run by an unprivileged user.
# getcap `which ping` /usr/bin/ping = cap_net_raw+ep