bool tcp_socket::bind(const socket_address& addr) { struct sockaddr_in local; memset((char*)&local, 0, sizeof(local)); int accept_from = addr.address().empty()? INADDR_ANY : INADDR_LOOPBACK; local.sin_addr.s_addr = htonl(accept_from); local.sin_port = htons(addr.port()); local.sin_family = AF_INET; return ::bind(_sock, (struct sockaddr*)&local, sizeof(local)) != -1; }
bool udt_socket::connect(const socket_address& endpoint) { struct sockaddr_in remote; remote.sin_family = AF_INET; remote.sin_port = htons(endpoint.port()); inet_aton(endpoint.address().c_str(), &remote.sin_addr); int res = UDT::connect(_sock, (const sockaddr*)&remote, sizeof(remote)); if (res == UDT::ERROR) _sock = UDT::INVALID_SOCK; else setAsyncWrites(_sock, true); return res == 0; }
/* This code is subject to the terms of the Mozilla Public License, v.2.0. http://mozilla.org/MPL/2.0/. */ #include "unittest.h" #include "socket_address.h" TEST_CASE( "socket_addressTest/testConstructors", "[unit]" ) { { socket_address addr("1.2.3.4", 5); assertEquals( "1.2.3.4", addr.address() ); assertEquals( 5, addr.port() ); assertEquals( "1.2.3.4:5", addr.toString() ); } { socket_address addr("localhost", 50000); assertEquals( "localhost", addr.address() ); assertEquals( 50000, addr.port() ); assertEquals( "localhost:50000", addr.toString() ); } { socket_address addr; assertEquals( "", addr.address() ); assertEquals( 0, addr.port() ); assertEquals( ":0", addr.toString() ); } } TEST_CASE( "socket_addressTest/testFromString", "[unit]" ) { socket_address addr; assertTrue( addr.fromString("1.2.3.4:5") );
bool udp_socket::setEndpoint(const socket_address& endpoint) { _endpoint.sin_family = AF_INET; _endpoint.sin_port = htons(endpoint.port()); return inet_aton(endpoint.address().c_str(), &_endpoint.sin_addr) != 0; }