示例#1
0
TcpStream TcpStream::connect(const Ipv4Address& address)
{
    Descriptor fd(socket(AF_INET, SOCK_STREAM, 0));
    if (fd < 0)
    {
        throw SystemError("IPv4 socket error");
    }
    if (::connect(fd, address.address(), address.length()) < 0)
    {
        throw SystemError("IPv4 connect error");
    }
    return TcpStream(std::move(fd));
}
示例#2
0
Ipv4Listener::Ipv4Listener(uint16_t port) :
    _fd(socket(AF_INET, SOCK_STREAM, 0))
{
    if(_fd < 0)
    {
        throw SystemError("IPv4 socket error");
    }
    Ipv4Address address = Ipv4Address::any(port);
    if(bind(_fd, address.address(), address.length()) < 0)
    {
        throw SystemError("IPv4 bind error");
    }
    if(listen(_fd, 20) < 0)
    {
        throw SystemError("IPv4 listen error");
    }
}