Related: C++ Winsock API how to get connecting client IP before accepting the connection?
Hi, when you are running a TCP server (written in C, using the Berkeley Socket API) is it possible to read a client's IP address/port before actually accepting the connection?
As far as I know you have to accept
the connection first and shutdown
it directly thereafter, if you don't want to communicate with a given client because of its IP address.
Pseudo-code (I am looking for the peek
and refuse
method):
int serverfd = listen(...);
for(;;) {
struct sockaddr_in clientAddr;
peek(serverfd, &clientAddr, sizeof(clientAddr));
if(isLegit(&clientAddr)) {
int clientfd = accept(serverfd, &clientAddr, sizeof(clientAddr));
handleClient(clientfd);
} else {
refuse(serverfd, &clientAddr, sizeof(clientAddr));
}
}
ioctl
for this. No dice. I don't think it can be done either. – Describe