示例#1
0
 void Socket::Connect(const char *url, unsigned short port)
 {
   sockaddr_in Addr = { 0 };
   FillAddr(url, port, &Addr);
   if (::connect(SockHandle, reinterpret_cast<sockaddr *>(&Addr), sizeof(Addr)) == -1)
     throw SocketException("Error connect socket");
 }
示例#2
0
int32 CSocket::SetLocalAddrAndPort(const string &strLocalAddress, uint16 u16LocalPort)
{
    sockaddr_in localAddr;

    FillAddr(strLocalAddress, u16LocalPort, localAddr);
    return bind(m_streamFd, (sockaddr *) &localAddr, sizeof(sockaddr_in));
}
示例#3
0
int32 CUdpSocket::SendTo(const void *pBuf, uint32 u32BufLen, 
    const string &strForeignAddr, uint16 u16ForeignPort)
{
    sockaddr_in destAddr;

    FillAddr(strForeignAddr, u16ForeignPort, destAddr);
    return sendto(m_streamFd, (raw_type *) pBuf, u32BufLen, 0,
        (sockaddr *) &destAddr, sizeof(destAddr));
}
示例#4
0
void CSocket::BindLocalAddressAndPort( const string &localAddress, unsigned short localPort /*= 0*/ )
    throw(CSocketException)
{
    // Get the address of the requested host
    sockaddr_in localAddr;
    FillAddr(localAddress, localPort, localAddr);
 
    if (bind(m_sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
        throw CSocketException("Set of local address and port failed (bind())", true);
    }
}
示例#5
0
void CSocket::ConnectToHost( const string &foreignAddress, unsigned short foreignPort ) throw(CSocketException)
{
    //cout<<"\nstart Connect to host";
    // Get the address of the requested host
    sockaddr_in destAddr;
    //cout<<"\ninside Connect to host";
    FillAddr(foreignAddress, foreignPort, destAddr);
 
    //cout<<"trying to connect to host";
    // Try to connect to the given port
    if (::connect(m_sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) {
        throw CSocketException("Connect failed (connect())", true);
    }
    //cout<<"\n after connecting";
 
}
示例#6
0
int32 CCommunicatingSocket::Connect(const string &strForeignAddr, uint16 u16ForeignPort)
{
    sockaddr_in destAddr;
    int32 i32Ret = 0;
    int32 i32Error = 0;
    int32 i32Len = 0;
    timeval tm;
    fd_set set;
    
    m_strForeignAddr = strForeignAddr;
    m_u16ForeignPort = u16ForeignPort;
    
    FillAddr(strForeignAddr, u16ForeignPort, destAddr);
    
    SetBlock(FALSE);
    if(connect(m_streamFd, (struct sockaddr *)&destAddr, sizeof(destAddr)) == -1)
    {
        tm.tv_sec = CONNECT_TIME_OUT_TIME;
        tm.tv_usec = 0;
        FD_ZERO(&set);
        FD_SET(m_streamFd, &set);
        if(select(m_streamFd+1, NULL, &set, NULL, &tm) > 0)
        {
            i32Len = sizeof(int32);
            getsockopt(m_streamFd, SOL_SOCKET, SO_ERROR, &i32Error, (socklen_t *)&i32Len);
            if(i32Error == 0)
            {
                i32Ret = 0;
            }
            else
            {
                i32Ret = -1;
            }
        } 
        else
        {
            i32Ret = -1;
        }
    }    
    SetBlock(TRUE);
    
    return i32Ret;
}
示例#7
0
sockaddr_in NetworkAddress::Sockaddr() const {
  sockaddr_in addr;
  FillAddr(&addr);
  return addr;
}