getsockname


 
Thread Tools Search this Thread
Top Forums Programming getsockname
# 1  
Old 10-11-2005
getsockname

int getPeerIP(int remoteConnFd )
{

char *sIPAddr;
sockaddr_in theirAddr;
int sin_size = sizeof(sockaddr_in);
int nRetVal = getsockname(remoteConnFd, (sockaddr *)&theirAddr, (socklen_t *)&sin_size);


if(nRetVal == -1)
{
printf("\n THERE IS ERROR IN HANDLING THE MEESAGE \n");
}
else
strcpy(sIPAddr,inet_ntoa(theirAddr.sin_addr));


printf("\n THE GETPEERIP IS RETURNING THE VALUE %s \n",sIPAddr);

return 0;
}



THIS IS THE FUNCTION I AM USING
MY PROBLEM IS if i print sIPAddr i get 127.0.0.1

I HAVE MADE THE CLIENT AND SERVER ON THE SAME TERMINAL

PLEASE TELL ME WHAT IS THIS FUNCTION FOR AND HOW IT IS DIFFERENT FROM GETPEERNAME

IF POSSIBLE THEN WITH EXAMPLE
# 2  
Old 10-11-2005
I'm not sure what you want.

My guess is:
getsockname returns the IP address of where the socket lives, i.e., the host it's connected to, in this case localhost. ie., the LOCAL address of the socket.

I'm guessing you want something else. mac address?

Code:
/*
 * gethwaddr.c
 *
 * Demonstrates retrieving hardware address of adapter using ioctl()
 *

 *
 */
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
/* linux only */

int main( int argc, char *argv[] )
{
        int s;
        struct ifreq buffer;

        s = socket(PF_INET, SOCK_DGRAM, 0);

        memset(&buffer, 0x00, sizeof(buffer));

        strcpy(buffer.ifr_name, "eth0");

        ioctl(s, SIOCGIFHWADDR, &buffer);

        close(s);

        for( s = 0; s < 6; s++ )
        {
                printf("%.2X ", (unsigned char)buffer.ifr_hwaddr.sa_data[s]);
        }

        printf("\n");

        return 0;
}

# 3  
Old 10-11-2005
Normally there are two computers involved in a socket. getsockname gets the IP address of the local endpoint of the socket. getpeername gets the IP address at the other end. When client and server both run of the same box, both calls can return 127.0.0.1 but that is a special case.
# 4  
Old 10-11-2005
You're correct, but I'm still not sure that's what he wants.

If he wanted getpeername() that's what he should call. He seems to know about it.
I know some implementations don't support X25 AF_CCITT (XOPEN standards).

I'm still confused.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question