コード例 #1
0
TEST_F (SocketAddressTest, Set)
{
    SocketAddress sa;
    ASSERT_TRUE (sa.SetToLocalhost (AF_INET, 1138));
    ASSERT_STREQ ("127.0.0.1", sa.GetIPAddress ().c_str ());
    ASSERT_EQ (1138, sa.GetPort ());

    ASSERT_TRUE (sa.SetToAnyAddress (AF_INET, 0));
    ASSERT_STREQ ("0.0.0.0", sa.GetIPAddress ().c_str ());
    ASSERT_EQ (0, sa.GetPort ());

    ASSERT_TRUE (sa.SetToLocalhost (AF_INET6, 1139));
    ASSERT_STREQ ("::1", sa.GetIPAddress ().c_str ());
    ASSERT_EQ (1139, sa.GetPort ());
}
コード例 #2
0
ファイル: socketbase.cpp プロジェクト: RonberKing/IosExample
void
SocketBase::GetPeerName(IPAddress &cRemoteAddr, int &iRemotePort)
{
    SocketAddress   cRemote;
    socklen_t       slDummy = cRemote.SizeOf();

    WaitMutex();

    if (getpeername(iSockDesc, (sockaddr *)cRemote, &slDummy) < 0)
    {
        ClearMutex();
        switch (errno)
        {
            case EBADF:
            case ENOTSOCK:  throw SocketException(SocketException::errBadDescriptor);
            case ENOBUFS:   throw SocketException(SocketException::errKernelMemory);
            case EFAULT:    throw SocketException(SocketException::errIllegalPointer);
            default:        throw SocketException(SocketException::errUnknown, errno);
        }
    }
    ClearMutex();

    cRemote.GetIPAddress(cRemoteAddr);
    cRemote.GetPortNumber(iRemotePort);
}
コード例 #3
0
ファイル: socketbase.cpp プロジェクト: RonberKing/IosExample
int
SocketBase::RecvFrom(void *pBuffer, int iBufLength, unsigned int uiFlags,
                     IPAddress &cSourceIP, int &iSourcePortNumber)
{
    SocketAddress   cSource;
    socklen_t       slDummy = cSource.SizeOf();
    int             iBytesRead;

    WaitMutex();

    if ((iBytesRead = recvfrom(iSockDesc, pBuffer, iBufLength, uiFlags, (sockaddr *)cSource, &slDummy)) < 0)
    {
        ClearMutex();
        switch (errno)
        {
            case EBADF:
            case ENOTSOCK:  throw SocketException(SocketException::errBadDescriptor);
            case ENOTCONN:  throw SocketException(SocketException::errNotConnected);
            case EAGAIN:    throw SocketException(SocketException::errWouldBlock);
            case EINTR:     throw SocketException(SocketException::errInterrupted);
            case EFAULT:    throw SocketException(SocketException::errIllegalPointer);
            case EINVAL:    throw SocketException(SocketException::errInvalidArgument);
            default:        throw SocketException(SocketException::errUnknown, errno);
        }
    }
    ClearMutex();

    if (iBytesRead)
    {
        cSource.GetIPAddress(cSourceIP);
        cSource.GetPortNumber(iSourcePortNumber);
        return iBytesRead;
    }
    throw SocketException(SocketException::errNotConnected);
}
コード例 #4
0
ファイル: socketbase.cpp プロジェクト: RonberKing/IosExample
int
SocketBase::protAccept(IPAddress &cRemoteAddr, int &iRemotePortNo)
{
    int           iNewSockDesc;
    SocketAddress cRemote;
    socklen_t     slDummy = cRemote.SizeOf();

    WaitMutex();

    if ((iNewSockDesc = accept(iSockDesc, (sockaddr *) cRemote, &slDummy)) < 0)
    {
        ClearMutex();
        switch (errno)
        {
            case EBADF:
            case ENOTSOCK:      throw SocketException(SocketException::errBadDescriptor);
            case EAFNOSUPPORT:  throw SocketException(SocketException::errNotStreamSock);
            case EFAULT:        throw SocketException(SocketException::errIllegalPointer);
            case EAGAIN:        throw SocketException(SocketException::errNoPendingConnections);
            case EPERM:         throw SocketException(SocketException::errFirewall);
            case ENOMEM:        throw SocketException(SocketException::errMemory);
            default:            throw SocketException(SocketException::errUnknown, errno);
        }
    }
    ClearMutex();

    cRemote.GetIPAddress(cRemoteAddr);
    cRemote.GetPortNumber(iRemotePortNo);

    return iNewSockDesc;
}