Question

I am writing a TCP server application using Winsock. The client that connects to me (for which I have no source and therefore cannot make changes) behaves as follows: If it connects and I close the connection (because it is an undesired client), it will immediately reconnect. As a result of this, I will spin like mad over my accept() calls if there's a client out there that wants to connect to me but whom I don't want to talk to (i.e. what I've called an undesired client).

I would like to find a way to prevent that mad spinning over my accept() calls. If there were only 1 desired client, I could bind my listening socket to that client's IP address rather than INADDR_ANY. But alas, there is more than 1 desired client. As far as I know, there's no way to allow connections from only a set of desired IP addresses when the size of that set is greater than 1.

I've looked at WSAAccept. Unfortunately, it looks like this would merely shift the mad spinning to a callback.

Is there a way to reject an attempted connection that doesn't involve accept() waking up and returning (and therefore leading to all this mad spinning)?

Was it helpful?

Solution

I believe your question has been already asked on stackoverflow in the past. Maybe looking at these two threads will help you with your problem :

If you're afraid to experience Dos attacks or about performance issues, accept is acting on the level 4 of the OSI stack, a relatively low level which is handling the SYN-ACK negociation with the client, so a basic connection from the outside immediatly dropped after an accept() will not cause important performance issues.

If, however, you want absolutely to accept two IP adresses I think you may have to use either a firewall or RAW sockets to retrieve directly the IP address from the TCP packet and accept the connection yourself, but this will be, I think, a huge loss of time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top