简体   繁体   中英

How do i display client's ip address in the report using c socket program?

I server report need to display the clients ip address in the report which is processed by the server,that is i need to display from which client the report is being sent. How do i do this? Can anybody help me out?

use this :

char clntName[INET6_ADDRSTRLEN];
char portName[6]; 

if (getnameinfo(&client_address,
                sizeof client_address,
                clntName,
                sizeof(clntName),
                NULL,
                0,
                NI_NUMERICHOST|NI_NUMERICSERV|NI_NUMERICSCOPE) == 0) {
    printf("Client = %s/%s\n",clntName,portName);
} else {
    printf("Unable to get address\n");
}

The 'remoteIp' char array in following code will give you the client's ip address, whcih you can print using string functions. 'fd' is the listen socket for your server port..

struct sockaddr_in remoteAddr;
int size = sizeof(remoteAddr);
char remoteIp[10] = {0};
acceptFd = accept(fd, (struct sockaddr *)&remoteAddr, &size);
inet_ntop(AF_INET, &remoteAddr.sin_addr, remoteIp, INET_ADDRSTRLEN)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM