当前位置: 首页>>代码示例>>C++>>正文


C++ IPAddress::IsValid方法代码示例

本文整理汇总了C++中IPAddress::IsValid方法的典型用法代码示例。如果您正苦于以下问题:C++ IPAddress::IsValid方法的具体用法?C++ IPAddress::IsValid怎么用?C++ IPAddress::IsValid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IPAddress的用法示例。


在下文中一共展示了IPAddress::IsValid方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

/* This little program demonstrates the parsing of IPAddress strings */
int main(int argc, char ** argv)
{
   CompleteSetupSystem css;

   PrintExampleDescription();

   while(1)
   {
      printf("Please enter a string representing an IPv4 or IPv6 numeric host-address: ");
      fflush(stdout);

      char buf[1024];
      (void) fgets(buf, sizeof(buf), stdin);

      String s = buf;
      s = s.Trim();  // get rid of newline ugliness

      IPAddress ip;
      if (ip.SetFromString(s) == B_NO_ERROR)
      {
         printf("I parsed the string [%s] as IPAddress %s\n", s(), ip.ToString()());
         printf("    ip.IsValid() returned %i\n", ip.IsValid());
         printf("    ip.IsIPv4() returned %i\n", ip.IsIPv4());
         printf("    ip.IsMulticast() returned %i\n", ip.IsMulticast());
         printf("    ip.IsStandardLoopbackAddress() returned %i\n", ip.IsStandardLoopbackDeviceAddress());
         printf("\n");
      }
      else printf("Error, couldn't parse [%s] as an IPAddress!\n", s());
   }
   return 0;
}
开发者ID:mtl1979,项目名称:muscle,代码行数:32,代码来源:example_2_interactive_ipaddress.cpp

示例2: NetworkException

/** @details The broadcast address can only be calculated on a IPv4 network.  While it is
 *  technically possible to calculate an IPv6 broadcast address, the IPv6 protocol has dropped
 *  support for broadcasting.  Since broadcasting is not supported in IPv6, this method will result
 *  in an exception given an IPv6 address.
 *  @param ip An IP address on the network.
 *  @param netmask The netmask defining the network range.
 *  @returns The broadcast address.
 */
_CGUL_EXPORT CGUL::Network::IPAddress CGUL::Network::IPAddress::CalculateBroadcast(const IPAddress& ip, const IPAddress& netmask)
{
    if (ip.GetType() == IPAddressType::IPV6 || netmask.GetType() == IPAddressType::IPV6)
    {
        throw NetworkException(NetworkExceptionCode::FAILED_CALCULATE_ADDRESS, NetworkExceptionReason::ADDRESS_INVALID);
    }
    if (!ip.IsValid() || ip.GetType() != netmask.GetType())
    {
        throw NetworkException(NetworkExceptionCode::FAILED_CALCULATE_ADDRESS, NetworkExceptionReason::ADDRESS_MISMATCH);
    }

    return IPAddress(ip.ToUInt32() | (~netmask.ToUInt32()));
}
开发者ID:Zethes,项目名称:CGUL,代码行数:21,代码来源:IPAddress.cpp

示例3: NetworkException

/** @brief Connects to a server on a given ip and port.
 *  @param ip The IP address to connect to.
 *  @param port The port number.
 */
void Jatta::Network::SocketTCP::Connect(const IPAddress& ip, unsigned short port)
{
    // Check that the IP is valid
    if (!ip.IsValid())
    {
        throw NetworkException(NetworkExceptionCode::FAILED_CONNECT, NetworkExceptionReason::ADDRESS_INVALID);
    }

    // Create a hints variable used to determine the connection configuration.
    struct addrinfo hints;
    memset(&hints, 0, sizeof(addrinfo));

    // Check if the IP is an IPv4 or IPv6.
    if (ip.GetType() == IPAddressType::IPV4)
    {
        // Use IPv4.
        hints.ai_family = AF_INET;
    }
    else
    {
        // Use IPv6.
        hints.ai_family = AF_INET6;
    }

    // We're setting up a TCP/IP connection, which is a STREAM socket.
    hints.ai_socktype = SOCK_STREAM;

    // Convert the port into a string.
    char portString[6];
#   ifdef MSVC
    sprintf_s(portString, "%d", port);
#   else
    sprintf(portString, "%d", port);
#   endif

    // Get the address info using the hints.
    addrinfo* result;
    if (getaddrinfo(ip.ToString().GetCString(), portString, &hints, &result) != 0)
    {
        throw NetworkException(NetworkExceptionCode::FAILED_CONNECT, NetworkExceptionReason::NO_NETWORK_INTERFACE);
    }

    // Create the socket.  Because our hints are so strict, we don't have to worry about looping
    // through the linked list.  We should be able to trust that the first result is what we want.
    sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (sock == INVALID_SOCKET)
    {
        freeaddrinfo(result);
        throw NetworkException(NetworkExceptionCode::FAILED_CONNECT, NetworkExceptionReason::FAILED_CREATE_SOCKET);
    }

    // Make the connection.
    if (::connect(sock, result->ai_addr, result->ai_addrlen) == SOCKET_ERROR)
    {
        freeaddrinfo(result);
        Close();
        throw NetworkException(NetworkExceptionCode::FAILED_CONNECT, NetworkExceptionReason::FAILED_CONNECT_CALL);
    }

    // Make a non-blocking socket.
    if (!MakeNonBlocking())
    {
        freeaddrinfo(result);
        Close();
        throw NetworkException(NetworkExceptionCode::FAILED_CONNECT, NetworkExceptionReason::FAILED_NONBLOCKING);
    }

    // Turn off the Nagle Algorithm to increase speed.
    if (!MakeNoDelay())
    {
        freeaddrinfo(result);
        Close();
        throw NetworkException(NetworkExceptionCode::FAILED_CONNECT, NetworkExceptionReason::FAILED_NO_DELAY);
    }

    // Free up the address info linked list.
    freeaddrinfo(result);
}
开发者ID:JoshuaBrookover,项目名称:Jatta,代码行数:82,代码来源:SocketTCP.cpp


注:本文中的IPAddress::IsValid方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。