Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the client IP address before accepting the connection in C++

Tags:

c++

sockets

ip

I'm studing c++ socket programming... The server program binds to a socket and starts listening for connection requests...ok now how can I list the IP addreses of the listened requests? I know I can get the IP addresses after accepting the connections but lets say I don't wanna accept a connection from an specific IP address...

like image 686
Mortie Avatar asked May 23 '12 20:05

Mortie


People also ask

How do I find the client IP address?

To get the client's public IP address, JavaScript acts as a third-party server-side language. JavaScript can't do it alone, so, jQuery is added to do this. JavaScript works with third-party applications to fetch the IP addresses.

How does a server know client IP?

For server IP address, the ServerSocket class uses the IP address of the local network interface through which it received the incoming request. Then, to obtain the remote client IP address, it decodes the IP header of the received TCP packet and uses the source address.


2 Answers

On Windows only, you can use the conditional callback feature of WinSock2's WSAAccept() function to access client information before accepting a connection, and to even reject the connection before it is accepted.

like image 158
Remy Lebeau Avatar answered Sep 22 '22 18:09

Remy Lebeau


This can't be done in terms of the standard socket API. On all platforms I know, the system actually accepts the connection (i.e. responds with SYN+ACK TCP datagram) before the application has a chance to monitor the pending request.

like image 31
valdo Avatar answered Sep 20 '22 18:09

valdo