Click here to Skip to main content
Rate this: bad
good
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 21:52pm
Rate this: bad
good
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.
  Permalink  
Comments
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 ( http://linux.die.net/man/3/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 319
1 _Amy 310
2 Nirav Prabtani 263
3 Sergey Alexandrovich Kryukov 210
4 Richard MacCutchan 125
0 OriginalGriff 7,889
1 Sergey Alexandrovich Kryukov 7,006
2 Maciej Los 4,014
3 Peter Leow 3,708
4 CHill60 2,762


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 22 Jul 2012
Copyright © CodeProject, 1999-2014
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