目的可能是为了获取rtmp真正的推流ip
如果rtmp推流地址是域名,往CDN推流的话,需要nslookup 的那种DNS解析,然后获取的几个ip
可以使用netstat -n 等命令查看 1935端口的真正ip是多少,废话不多说,下面是代码:
--------------------
代码中可能只需要关心 EnumTCPTable() 然后判断 if (htons((u_short)pTcpTable->table[i].dwRemotePort) == 1935)
获取该端口的ip
-------------------------------------------------------------------------------------------------------------------------
#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <stdio.h> #include <Iphlpapi.h> #include <malloc.h> #include <Winsock2.h> #include <stdlib.h> #include <tchar.h> #pragma comment(lib, "Iphlpapi.lib") #pragma comment(lib, "ws2_32.lib") static char TcpState[][32] = { "???", "CLOSED", "LISTENING", "SYN_SENT", "SEN_RECEIVED", "ESTABLISHED", "FIN_WAIT", "FIN_WAIT2", "CLOSE_WAIT", "CLOSING", "LAST_ACK", "TIME_WAIT" }; DWORD EnumTCPTable() { PMIB_TCPTABLE pTcpTable = NULL; DWORD dwSize = 0; DWORD dwRetVal = ERROR_SUCCESS; struct in_addr rip; struct in_addr lip; char szrip[32] = {0}; char szlip[32] = {0}; //获得pTcpTable所需要的真实长度,dwSize if (GetTcpTable(pTcpTable, &dwSize, TRUE) == ERROR_INSUFFICIENT_BUFFER) { pTcpTable = (MIB_TCPTABLE*) malloc ((UINT) dwSize); } else return dwRetVal; printf("Active Connections\n\n"); printf(" Proto\t%-24s%-24s%s\n","Local Address","Foreign Address","State"); if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) == NO_ERROR) { for (int i = 0; i < (int) pTcpTable->dwNumEntries; i++) { rip.S_un.S_addr = pTcpTable->table[i].dwRemoteAddr; lip.S_un.S_addr = pTcpTable->table[i].dwLocalAddr; //监听端口,远程主机端口为0,但函数返回是有值的,不知道它是怎么考虑的 if (pTcpTable->table[i].dwState == MIB_TCP_STATE_LISTEN) pTcpTable->table[i].dwRemotePort = 0; //dwLocalPort,dwRemotePort 是网络字节 _snprintf(szlip,sizeof(szlip),"%s:%d",inet_ntoa(lip),htons((u_short)pTcpTable->table[i].dwLocalPort)); _snprintf(szrip,sizeof(szrip),"%s:%d",inet_ntoa(rip),htons((u_short)pTcpTable->table[i].dwRemotePort)); printf(" TCP\t%-24s%-24s%s\n",szlip,szrip,TcpState[pTcpTable->table[i].dwState]); } } else { printf("\tCall to GetTcpTable failed.\n"); LPVOID lpMsgBuf; if (FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL )) { printf("\tError: %s", lpMsgBuf); } LocalFree( lpMsgBuf ); } GlobalFree(pTcpTable); return dwRetVal; } DWORD EnumUDPTable() { PMIB_UDPTABLE pUdpTable = NULL; DWORD dwSize = 0; DWORD dwRetVal = ERROR_SUCCESS; // struct in_addr rip; struct in_addr lip; // char szrip[32] = {0}; char szlip[32] = {0}; //获得pUdpTable所需要的真实长度,dwSize if (GetUdpTable(pUdpTable, &dwSize, TRUE) == ERROR_INSUFFICIENT_BUFFER) { pUdpTable = (MIB_UDPTABLE*) malloc ((UINT) dwSize); } else return dwRetVal; printf("Active Connections\n\n"); printf(" Proto\t%-24s%-24s\n","Local Addr","Local Port"); if ((dwRetVal = GetUdpTable(pUdpTable, &dwSize, TRUE)) == NO_ERROR) { for (int i = 0; i < (int) pUdpTable->dwNumEntries; i++) { // rip.S_un.S_addr = pUdpTable->table[i].dwRemoteAddr; lip.S_un.S_addr = pUdpTable->table[i].dwLocalAddr; //监听端口,远程主机端口为0,但函数返回是有值的,不知道它是怎么考虑的 // if (pUdpTable->table[i].dwState == MIB_Udp_STATE_LISTEN) // pUdpTable->table[i].dwRemotePort = 0; //dwLocalPort,dwRemotePort 是网络字节 _snprintf(szlip,sizeof(szlip),"%s:%d",inet_ntoa(lip),htons((u_short)pUdpTable->table[i].dwLocalPort)); // _snprintf(szrip,sizeof(szrip),"%s:%d",inet_ntoa(rip),htons((u_short)pTcpTable->table[i].dwRemotePort)); // printf(" TCP\t%-24s%-24s%s\n",szlip,szrip,TcpState[pTcpTable->table[i].dwState]); printf(" UDP\t%-24s\n",szlip); } } else { printf("\tCall to GetUdpTable failed.\n"); LPVOID lpMsgBuf; if (FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL )) { printf("\tError: %s", lpMsgBuf); } LocalFree( lpMsgBuf ); } GlobalFree(pUdpTable); return dwRetVal; } DWORD DelTCPConnect(const char *lpszLocalAddr, const char * lpszRemoteAddr, DWORD dwLocalPort, DWORD dwRemotePort) { DWORD dwRetVal = ERROR_NOT_FOUND; MIB_TCPROW srtTcpRow; srtTcpRow.dwLocalAddr = inet_addr(lpszLocalAddr); srtTcpRow.dwRemoteAddr = inet_addr(lpszRemoteAddr); srtTcpRow.dwLocalPort = htons(dwLocalPort); srtTcpRow.dwRemotePort = htons(dwRemotePort); srtTcpRow.dwState = MIB_TCP_STATE_DELETE_TCB; //目前为止,settcpEntry只支持该参数 dwRetVal = SetTcpEntry(&srtTcpRow); if (dwRetVal != ERROR_SUCCESS) { LPVOID lpMsgBuf; if (FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL )) { printf("\tError: %s", lpMsgBuf); } LocalFree( lpMsgBuf ); } return dwRetVal; } void main() { DWORD TcpInfo = EnumTCPTable(); DWORD UdpInfo = EnumUDPTable(); system("pause"); system("pause"); }
http://blog.csdn.net/syytem004/article/details/43272359