C++ Winsock API how to get connecting client IP before accepting the connection?

前端 未结 4 960
慢半拍i
慢半拍i 2020-12-21 03:13

I am using the Winsock API (not CAsyncSocket) to make a socket that listens for incoming connections.

When somebody tries to connect, how can I get their IP address

4条回答
  •  攒了一身酷
    2020-12-21 04:01

    accept the connection, look at the IP, if it is not allowed, close the connection

    Edit:

    I'm assuming you're talking about TCP connection. When you listen to the port and a connection comes from a client, the API will perform the TCP 3-way handshake, and the client will know that this port is being listened to.

    I am not sure if there is a way to prevent sending any packets (i.e. accepting the connection) so that you can look at the IP address first and then decide.

    The only way I can think of is to do packet filtering based on the source IP at the network layer (using firewall, for example).

提交回复
热议问题