通过socket过去本地ip,port和远端ip,port

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);
    }
}

通过socket过去本地ip,port和远端ip,port

上一篇:GIT笔记


下一篇:Flyway Validate failed: Migration checksum mismatch for migration version 1.0.0.01 错误