1

I'm writing a simple client-server code in C. i was asked for the server to print the IP address of the client that connected to it. However, i can't seem to find a way to know the client's IP address from the server console. Is there a way to do that?

// Initialize Winsock.
if ( StartupRes != NO_ERROR )
{
    printf( "error %ld at WSAStartup( ), ending program.\n", WSAGetLastError() );
    // Tell the user that we could not find a usable WinSock DLL.                                  
    return;
}

/* The WinSock DLL is acceptable. Proceed. */

// Create a socket.    
MainSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( MainSocket == INVALID_SOCKET ) 
{
    printf( "Error at socket( ): %ld\n", WSAGetLastError( ) );
    return;
}

// Create a sockaddr_in object and set its values.
// Declare variables

Address = inet_addr(CHANNEL_IP);
if ( Address == INADDR_NONE )
{
    printf("The string \"%s\" cannot be converted into an ip address. ending program.\n",
            CHANNEL_IP);
    return;
}
service.sin_family = AF_INET;
service.sin_addr.s_addr = Address;
service.sin_port = htons(clientinfo->senderPort);

//Bind the socket
bindRes = bind( MainSocket, ( SOCKADDR* ) &service, sizeof( service ) );
if ( bindRes == SOCKET_ERROR ) 
{
    printf( "Channel-bind( ) failed with error %ld. Ending program\n", WSAGetLastError( ) );
    return;
}

 // Listen on the Socket.
ListenRes = listen( MainSocket, SOMAXCONN );
if ( ListenRes == SOCKET_ERROR ) 
{
    printf( "Failed listening on socket, error %ld.\n", WSAGetLastError() );
    return;
}
printf("Channel waiting for sender to connect...\n");

//Accepting connection
SenderSocket = accept( MainSocket, NULL, NULL );
    if ( SenderSocket == INVALID_SOCKET ){
        printf( "Accepting connection with client failed, error %ld\n", WSAGetLastError() ) ; 
        return;}
    else
        printf( "Sender Connected.\n" );
user128176
  • 23
  • 1
  • 5
  • Possible duplicate of [how to get ip address from sock structure in c ?](http://stackoverflow.com/questions/3060950/how-to-get-ip-address-from-sock-structure-in-c) – LPs Mar 20 '17 at 14:31
  • 1
    http://man7.org/linux/man-pages/man2/getpeername.2.html – midor Mar 20 '17 at 14:31
  • 1
    Two ways: When you accept the client you will get its address from the [`accept`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms737526(v=vs.85).aspx) function. If you throw it away you have the [`getpeername`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms738533(v=vs.85).aspx) function. – Some programmer dude Mar 20 '17 at 14:32
  • Or is your question about how to get an IP-address (and possible port number) from the `sockaddr_in` structure (which the *non-accepted* answer in the linked duplicate answers)? It's not really clear what your after. – Some programmer dude Mar 20 '17 at 14:34

1 Answers1

5

You need to pass in non-null values for the second and third parameters to accept:

struct sockaddr_in client_addr;
socklen_t slen = sizeof(client_addr);
SenderSocket = accept( MainSocket, (struct sockaddr *)&client_addr, &slen );

You can then get the client's IP and port from client_addr.

dbush
  • 205,898
  • 23
  • 218
  • 273