zoukankan      html  css  js  c++  java
  • //获取本地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 }
  • 相关阅读:
    Android_EditText
    JAVA_Gson_example
    JAVA_Gson
    JAVA_eclipse 保留Java文件时自动格式化代码和优化Import
    JAVA_JSON_example
    JAVA_JSON
    JAVA_HttpClientUtils
    Android_Gallery
    JAVA_JDBC
    day05 Pyhton学习
  • 原文地址:https://www.cnblogs.com/xuejianhui/p/2678480.html
Copyright © 2011-2022 走看看