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

前端 未结 4 843
猫巷女王i
猫巷女王i 2020-12-21 03:31

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 03:51

    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).

提交回复
热议问题