Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ Linux Network
how can i get client ip address in AF_INET ?
I want to write a program does as a listener!
and log input information!
Posted 21-Jul-12 22:52pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Not completely sure this is what you mean, but you can specify a struct to the accept[^] call (you use this to accept connection requests from clients) that is filled with the client's IP and port. Google for "accept socket" for more information and possible examples.
Bathooman at 22-Jul-12 5:46am
i meant if i want to write a program that listen on a specific port and i want to log any ip address that try to connect my listening server
Code-o-mat at 22-Jul-12 5:55am
Then I still say what I originally wrote should work.
Bathooman at 24-Jul-12 7:47am
struct sockaddr_in cltaddr;
cs = accept(sock ,(struct sockaddr *)&cltaddr ,&addrlen);
printf("%d" ,(cltaddr.sin_addr.s_addr);

how can i show the client ip address in network byte order?
Code-o-mat at 24-Jul-12 9:20am
How about the function inet_ntoa ( )? You can also see it being used in the examples shown to you by Volynsky Alex (Solution 2).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 250
1 Sergey Alexandrovich Kryukov 204
2 PIEBALDconsult 180
3 Suvendu Shekhar Giri 115
4 Peter Leow 100
0 Sergey Alexandrovich Kryukov 6,434
1 OriginalGriff 6,033
2 Peter Leow 2,534
3 Maciej Los 2,268
4 Abhinav S 2,264

Advertise | Privacy | Mobile
Web03 | 2.8.150414.1 | Last Updated 22 Jul 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100