struct sockaddr addr;
struct sockaddr_in* addr_v4;
int addr_len = sizeof(addr);
//获取local ip and port
ZeroMemory(&addr, sizeof(addr));
if (0 == getsockname(s, &addr, &addr_len))
{
if (addr.sa_family == AF_INET)
{
addr_v4 = (sockaddr_in*)&addr;
cMonitorInf.nSourceIP = addr_v4->sin_addr.S_un.S_addr;
cMonitorInf.nSourcePort = ntohs(addr_v4->sin_port);
}
}
//获取remote ip and port
ZeroMemory(&addr, sizeof(addr));
if (0 == getpeername(s, &addr, &addr_len))
{
if (addr.sa_family == AF_INET)
{
addr_v4 = (sockaddr_in*)&addr;
cMonitorInf.nDesIP = addr_v4->sin_addr.S_un.S_addr;
cMonitorInf.nDesPort = ntohs(addr_v4->sin_port);
}
}
转自:http://bbs.sciencenet.cn/home.php?mod=space&uid=433431&do=blog&id=329335