I have this select waiting for data in the socket buffer. When there is data in the buffer, I launch a thread and accept the connection.
while (!InterruptWaitForConnections)
{
const int status = select(ListenSocket+1, &readfds, writefds, exceptfds, timeout);
if (status == -1)
{
// ERROR: do something
return false;
}
else if (status > 0)
{
// - The socket buffer has data
// - Check if it is a valid connection
// - Reject or accept
return true;
}
else if(status == 0)
{
//timeout
}
}
I would like to filter connections before accepting them. I only want to accept connections that come from my client.
If a connection comes from somewhere else or there is garbage in the socket buffer I simply want to clear the socket buffer.
My client can be on different IPs. So I need to know a way to recognize my client.
Does anyone know how I can do that?
I would like to filter connections before accepting them. I only want to accept connections that come from my client.
The only way (that I know of) to do that with Winsock is to use WSAAccept()
with a condition callback function that decides whether to accept or reject each pending connection.
Otherwise, you will just have to accept()
each connection and then decide whether to close it or not based on whether it meets your desired criteria.