user960456
user960456

Reputation: 141

How to determine which IP address is in use after accept?

If I wrote a server bound to a wildcard address (INADDR_ANY), how to determine which IP address the client is connected to?

I tried the following code when the after a successful accept call, but it just returned 0.0.0.0.

inet_ntop(AF_INET, &server_address.sin_addr, s, sizeof(s));

Upvotes: 1

Views: 355

Answers (2)

John
John

Reputation: 449

the accept system call will have a address of type struct sockaddr_in or struct sockaddr_in6.

the client address will be stored there.

you can get it using inet_ntoa, or inet_ntop.

Upvotes: 0

Sebastian
Sebastian

Reputation: 8164

As stated in How to determine IP used by client connecting to INADDR_ANY listener socket in C, use getsockname, which gives you the socket which the connection is bound to (on local level).

This is for C, but is applicable for C++ to.

Upvotes: 4

Related Questions