//获取本地IP地址和对端IP地址
1 //获取本地IP地址 2 CHAR * GetLocalIpAdrr(int fd) 3 { 4 struct sockaddr_in stHost; 5 memset(&stHost, 0, sizeof(stHost)); 6 7 #ifdef _WIN32 8 INT iLen = sizeof(stHost); 9 #else 10 socklen_t iLen = sizeof(stHost); 11 #endif 12 CHAR * szHostIp; 13 14 if(0 != getsockname((UINT)fd, (sockaddr *)&stHost, &iLen)) 15 { 16 g_enErrorCode = PU_ERROR_CODE_SOCKET_ERROR; 17 return NULL; 18 } 19 szHostIp = inet_ntoa(stHost.sin_addr); 20 21 return szHostIp; 22 } 23 24 //获取对端IP地址 25 CHAR * GetRemoteIpAddr(int fd) 26 { 27 struct sockaddr_in stRemote; 28 memset(&stRemote, 0, sizeof(stRemote)); 29 30 #ifdef _WIN32 31 INT iLen = sizeof(stRemote); 32 #else 33 socklen_t iLen = sizeof(stRemote); 34 #endif 35 CHAR * szRemoteIp; 36 37 if (0 != getpeername((UINT)fd, (sockaddr *)&stRemote, &iLen)) 38 { 39 g_enErrorCode = PU_ERROR_CODE_SOCKET_ERROR; 40 return NULL; 41 } 42 szRemoteIp = inet_ntoa(stRemote.sin_addr); 43 return szRemoteIp; 44 }
posted on 2012-09-10 11:26 xuejianhui 阅读(1426) 评论(0) 编辑 收藏 举报