I am programming a UDP Relaying Server in C++. But I have a problem.
I have a basic loop that just calls recvfrom()
, checks for errors in the packet, then reads the "target" out of the packet and uses sendto()
on the same socket to send a packet to the target client that is also relaying on the same server.
The problem is that nearly all packets get lost if I don't add a delay before the sendto()
(this delay depends on the connection speed, so I can´t set it statically).
m_iSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
bind(m_iSocket, (SOCKADDR*)&m_oSockAddress, sizeof(SOCKADDR_IN));
...
while(true) {
recvfrom(m_iSocket, (char*)m_pRecvBuffer, m_nBufferSize, 0, (SOCKADDR*)&remoteAddr, &remoteAddrLen);
...
sendto(m_iSocket, reinterpret_cast<char*>(rw.getData()), rw.getBufferSize(), 0, targets address , address size);
}
Any ideas?