how do you cast sockaddr structure to a sockaddr_in - C++ networking sockets ubuntu UDP

Viewed 27860

I am trying to get the client address, but i am unsure how do i cast the sockaddr structure to sockaddr_in?

struct sockaddr_in cliAddr, servAddr;

    n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *) cliAddr,sizeof(cliAddr));

 //i tried this but it does not work
    struct sockaddr cliSockAddr = (struct sockaddr *) cliAddr; 
    char *ip = inet_ntoa(cliSockAddr.sin_addr);

Thanks in advance! :)


i've found questions that brought me to this step: Getting IPV4 address from a sockaddr structure


Sorry to avoid confusion, this is my real implementation where "ci" is an object to store pointers such as sockaddr_in.

    /* receive message */
    n = recvfrom(*(ci->getSd()), msg, MAX_MSG, 0,(struct sockaddr *) ci->getCliAddr(),ci->getCliLen());

    char *ip = inet_ntoa(ci->getCliAddr().sin_addr);

i will get the following errors:

udpserv.cpp:166: error: request for member ‘sin_addr’ in ‘ci->clientInfo::getCliAddr()’, which is of non-class type ‘sockaddr_in*’
3 Answers
Related