Find IP address from remote end of a TCP socket

by
on
October 31, 2008

In C or C++ it is fairly simple to find the IP address of the remote end of a TCP socket.

The following example shows how to do this using the getpeername() and inet_ntoa() system calls.

int sockfd;
int len;
char * hostip;
struct sockaddr_in sin;

len = sizeof(sin);

if (0 != getpeername(sockfd, (struct sockaddr *) &sin, &len))
        perror("getpeername");
}

hostip = inet_ntoa(sin.sin_addr);

printf("client IP: %s\n", hostip);

No Comments
C
, , , , , , , ,

Related posts:

  1. Display hostname and IP address in C
  2. Find the MAC address on a Windows machine
  3. Format output using printf
  4. Find your IP address with ifconfig
  5. Find your MAC address with ifconfig

Comments (0)

No comments yet

Trackbacks (0)

No trackbacks yet

Leave a Comment

(displayed with your post)
(will not be published)
(optional)

Copyright 2008-2010 WiredRevolution.com. All rights reserved.