我正在尝试获取客户端地址,但我不确定如何将 sockaddr 结构转换为 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);
提前致谢!:)
我发现了让我走到这一步的问题: Getting IPV4 address from a sockaddr structure
很抱歉避免混淆,这是我真正的实现,其中“ci”是一个存储指针的对象,例如 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);
我会收到以下错误:
udpserv.cpp:166: error: request for member ‘sin_addr’ in ‘ci->clientInfo::getCliAddr()’, which is of non-class type ‘sockaddr_in*’