First, code of sender, that send 2000 packs 50 bytes every 300ms by timer
Why are you using
TIdUDPServer to send the test packets, instead of using
TIdUDPClient?
I write echo server, it receives pack and send back.
The correct way to send a reply back to the sender in the
OnUDPRead event is to use
ABinding.SendTo() instead of
TIdUDPServer.SendBuffer(). The
ABinding parameter represents the listening socket that actually received the packet and fired the
OnUDPRead event, so you should use the same socket to send the reply back. If you use
SendBuffer() instead, it will use the 1st socket in the
TIdUDPServer.Bindings collection, which might not be the correct socket if there are multiple bindings defined (such as listening on separate IPv4 + IPv6 sockets, or a separate socket per network interface, etc).
After that, I notice that not all packs are achieved the echo-server. For example I sended 100,000 packs, received 80,000 packs, but wireshark shows in receive side 100,000packs.
The most likely cause is your server reading packets slower than the sender is sending them, so the listening socket's buffer eventually fills up and discards packets. Unlike TCP, UDP does not guarantee delivery. You need to either slow down the rate of the sender, or speed up the rate of the reading, or increase the buffer size of the reader socket so it can hold more packets in cache, eg:
for I := 0 to IdUDPServer1.Bindings.Count-1 do begin
IdUDPServer1.Bindings[I].SetSockOpt(Id_SOL_SOCKET, Id_SO_RCVBUF, 1024*1024*10); // 10MB
end;
So, my question is how to make it work in duplex mode?
It already is. That is not the problem.
May be it is nedeed set big buffers
Most likely.
If by /simultaneous/, you mean "Sending while receiving", then you need >=2 ports.
That is not correct. A *socket* can send and read at the same time. A *thread* cannot, and the
OnUDPRead event is triggered in a worker thread. But you can multiple bindings running in parallel. And you can have other threads sending while the server is reading.
How to configure a socket or OS to accept and remember incoming packets while sending.
It already does. But it can hold only so many packets by default. If a new packet arrives and the socket's buffer is full, the packet is silently discarded.