Example #1
0
void
SocketBase::Bind(IPAddress &cLocalAddr, int iLocalPort)
{
    SocketAddress cLocal;

    cLocal.SetIPAddress(cLocalAddr);
    cLocal.SetPortNumber(iLocalPort);

    WaitMutex();

    if (bind(iSockDesc, (sockaddr *)cLocal, cLocal.SizeOf()) < 0)
    {
        ClearMutex();
        switch (errno)
        {
            case EBADF:
            case ENOTSOCK:      throw SocketException(SocketException::errBadDescriptor);
            case EINVAL:        throw SocketException(SocketException::errAlreadyBound);
            case EACCES:        throw SocketException(SocketException::errAddressProtected);
            case EADDRINUSE:    throw SocketException(SocketException::errAddrInUse);
            default:            throw SocketException(SocketException::errUnknown, errno);
        }
    }
    ClearMutex();
}
Example #2
0
int
SocketBase::SendTo(const void *pPayload, int iPayloadLength, unsigned int uiFlags,
                   IPAddress &cDestinationIP, int iDestinationPortNumber)
{
    SocketAddress   cDestination;
    int             iBytesRead;

    cDestination.SetIPAddress(cDestinationIP);
    cDestination.SetPortNumber(iDestinationPortNumber);

    WaitMutex();

    if ((iBytesRead = sendto(iSockDesc, pPayload, iPayloadLength, uiFlags, (sockaddr *)cDestination, cDestination.SizeOf())) < 0)
    {
        ClearMutex();
        switch (errno)
        {
            case EBADF:
            case ENOTSOCK:     throw SocketException(SocketException::errBadDescriptor);
            case EAGAIN:       throw SocketException(SocketException::errWouldBlock);
            case EINTR:        throw SocketException(SocketException::errInterrupted);
            case EFAULT:       throw SocketException(SocketException::errIllegalPointer);
            case EINVAL:       throw SocketException(SocketException::errInvalidArgument);
            case EMSGSIZE:     throw SocketException(SocketException::errMessageSizeTooBig);
            case ENOBUFS:
            case ENOMEM:       throw SocketException(SocketException::errKernelMemory);
            case EHOSTUNREACH: throw SocketException(SocketException::errHostUnreachable);
            case EPIPE:        throw SocketException(SocketException::errNotConnected);
            default:           throw SocketException(SocketException::errUnknown, errno);
        }
    }
    ClearMutex();

    return iBytesRead;
}
Example #3
0
void
SocketBase::Connect(IPAddress &cServAddr, int iServPortNo)
{
    SocketAddress cServer;

    cServer.SetIPAddress(cServAddr);
    cServer.SetPortNumber(iServPortNo);

    WaitMutex();

    if (connect(iSockDesc, (sockaddr *) cServer, cServer.SizeOf()) < 0)
    {
        ClearMutex();
        switch (errno)
        {
            case EBADF:
            case ENOTSOCK:      throw SocketException(SocketException::errBadDescriptor);
            case EFAULT:        throw SocketException(SocketException::errIllegalPointer);
            case EISCONN:       throw SocketException(SocketException::errAlreadyConnected);
            case ECONNREFUSED:  throw SocketException(SocketException::errConnectRefused);
            case ETIMEDOUT:     throw SocketException(SocketException::errConnectTimeOut);
            case ENETUNREACH:   throw SocketException(SocketException::errNetUnreachable);
            case EHOSTUNREACH:  throw SocketException(SocketException::errHostUnreachable);
            case EADDRINUSE:    throw SocketException(SocketException::errAddrInUse);
            case EINPROGRESS:   throw SocketException(SocketException::errInProgress);
            case EALREADY:      throw SocketException(SocketException::errAlreadyConnecting);
            case EAFNOSUPPORT:  throw SocketException(SocketException::errIncorrectAddrFamily);
            case EACCES:        throw SocketException(SocketException::errBrdCastNotEnabled);
            default:            throw SocketException(SocketException::errUnknown, errno);
        }
    }
    ClearMutex();
}