本文整理汇总了C++中SocketAddress类的典型用法代码示例。如果您正苦于以下问题:C++ SocketAddress类的具体用法?C++ SocketAddress怎么用?C++ SocketAddress使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SocketAddress类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TEST
TEST(AsyncSocketTest, duplicateBind) {
EventBase base;
auto server1 = AsyncServerSocket::newSocket(&base);
server1->bind(0);
server1->listen(10);
SocketAddress address;
server1->getAddress(std::addressof(address));
auto server2 = AsyncServerSocket::newSocket(&base);
EXPECT_THROW(server2->bind(address.getPort()), std::exception);
}
示例2:
HTTPClientSession::HTTPClientSession(const SocketAddress& address):
_host(address.host().toString()),
_port(address.port()),
_proxyPort(HTTPSession::HTTP_PORT),
_keepAliveTimeout(DEFAULT_KEEP_ALIVE_TIMEOUT, 0),
_reconnect(false),
_mustReconnect(false),
_expectResponseBody(false),
_pRequestStream(0),
_pResponseStream(0)
{
}
示例3: socket_sendto
nsapi_size_or_error_t AT_CellularStack::socket_sendto(nsapi_socket_t handle, const SocketAddress &addr, const void *data, unsigned size)
{
CellularSocket *socket = (CellularSocket *)handle;
if (!socket) {
return NSAPI_ERROR_DEVICE_ERROR;
}
if (socket->closed && !socket->rx_avail) {
tr_info("sendto socket %d closed", socket->id);
return NSAPI_ERROR_NO_CONNECTION;
}
if (size == 0) {
if (socket->proto == NSAPI_UDP) {
return NSAPI_ERROR_UNSUPPORTED;
} else if (socket->proto == NSAPI_TCP) {
return 0;
}
}
nsapi_size_or_error_t ret_val = NSAPI_ERROR_OK;
if (!socket->created) {
_at.lock();
ret_val = create_socket_impl(socket);
_at.unlock();
if (ret_val != NSAPI_ERROR_OK) {
tr_error("Socket %d create %s error %d", find_socket_index(socket), addr.get_ip_address(), ret_val);
return ret_val;
}
}
/* Check parameters */
if (addr.get_ip_version() == NSAPI_UNSPEC) {
return NSAPI_ERROR_DEVICE_ERROR;
}
_at.lock();
ret_val = socket_sendto_impl(socket, addr, data, size);
_at.unlock();
if (ret_val >= 0) {
tr_info("Socket %d sent %d bytes to %s port %d", find_socket_index(socket), ret_val, addr.get_ip_address(), addr.get_port());
} else if (ret_val != NSAPI_ERROR_WOULD_BLOCK) {
tr_error("Socket %d sendto %s error %d", find_socket_index(socket), addr.get_ip_address(), ret_val);
}
return ret_val;
}
示例4: bind
void TCPServerSocket::bind(const SocketAddress &localAddress)
throw(SocketException) {
createSocket(localAddress, SOCK_STREAM, IPPROTO_TCP);
if (::bind(sockDesc, localAddress.getSockaddr(),
localAddress.getSockaddrLen()) < 0)
throw SocketException(string("Call to bind() failed : ") +
strerror(errno));
// This is temporary.
setListen(5);
}
示例5: sendto
nsapi_size_or_error_t UDPSocket::sendto(const char *host, uint16_t port, const void *data, nsapi_size_t size)
{
SocketAddress address;
nsapi_size_or_error_t err = _stack->gethostbyname(host, &address);
if (err) {
return NSAPI_ERROR_DNS_FAILURE;
}
address.set_port(port);
// sendto is thread safe
return sendto(address, data, size);
}
示例6: CheckPrefixMatch
void CheckPrefixMatch(
const SocketAddress& first,
const SocketAddress& second,
unsigned matchingPrefixLen) {
unsigned i;
for (i = 0; i <= matchingPrefixLen; i++) {
EXPECT_TRUE(first.prefixMatch(second, i));
}
unsigned addrLen = (first.getFamily() == AF_INET6) ? 128 : 32;
for (; i <= addrLen; i++) {
EXPECT_TRUE(!first.prefixMatch(second, i));
}
}
示例7: TEST
TEST(SocketAddress, IPv4ToStringConversion) {
// testing addresses *.5.5.5, 5.*.5.5, 5.5.*.5, 5.5.5.*
SocketAddress addr;
for (int pos = 0; pos < 4; ++pos) {
for (int i = 0; i < 256; ++i) {
auto fragments = folly::make_array(5, 5, 5, 5);
fragments[pos] = i;
auto ipString = folly::join(".", fragments);
addr.setFromIpPort(ipString, 1234);
EXPECT_EQ(addr.getAddressStr(), ipString);
}
}
}
示例8: bind
void StreamServerSocket::bind( const SocketAddress& sockaddr ) {
if ( m_fd < 0 ) m_fd = socket( sockaddr.linkProtocol(), SOCK_STREAM, 0 );
if ( m_fd < 0 ) {
throw CException("Error creating socket", m_fd);
}
const struct sockaddr* raw = sockaddr.raw();
socklen_t socklen = sockaddr.rawlen();
int rc = ::bind( m_fd, raw, socklen );
if( rc ) {
throw BindException("Error on bind", errno);
}
}
示例9: connect
nsapi_error_t TCPSocket::connect(const char *host, uint16_t port)
{
SocketAddress address;
nsapi_error_t err = _stack->gethostbyname(host, &address);
if (err) {
return NSAPI_ERROR_DNS_FAILURE;
}
address.set_port(port);
// connect is thread safe
return connect(address);
}
示例10: networkState
//Al principio el estado de la red es UNINITIALIZED
NetworkClient::NetworkClient(std::string _addressServer, std::string _addressClient, std::string _nick):nick(_nick), networkState(NetworkState::UNINITIALIZED)
{
saServer.SetAddress(_addressServer);
SocketAddress myAddress;
myAddress.SetAddress(_addressClient);
int errBind = udpSocket.Bind(myAddress);
int errBlock = udpSocket.NonBlocking(true);
if (errBind > -1 && errBlock > -1)
{
//Si podemos hacer BIND y NONBLOCKING, pasamos a estado SAYINGHELLO
//Este cliente empezará a buscar al servidor.
networkState = NetworkState::SAYINGHELLO;
}
}
示例11: setMCastInterface
/*! Spcify the network interface for outgoing multicast
packages
*/
void DgramSocket::setMCastInterface(const SocketAddress &interf)
{
int rc = setsockopt(_sd,
IPPROTO_IP,
IP_MULTICAST_IF,
reinterpret_cast<SocketOptT*>(interf.getSockAddr()),
interf.getSockAddrSize());
if(rc < 0)
{
throw SocketError("setsockopt(IPPROTO_IP,IP_MULTICAST_IF)");
}
}
示例12: main
int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (FLAGS_port == 0) {
LOG(ERROR) << "Must specify port";
exit(EXIT_FAILURE);
}
// Prep the socket
EventBase evb;
AsyncSocket::UniquePtr socket;
if (FLAGS_ssl) {
auto sslContext = std::make_shared<SSLContext>();
socket = AsyncSocket::UniquePtr(new AsyncSSLSocket(sslContext, &evb));
} else {
socket = AsyncSocket::UniquePtr(new AsyncSocket(&evb));
}
socket->detachEventBase();
if (FLAGS_tfo) {
#if FOLLY_ALLOW_TFO
socket->enableTFO();
#endif
}
// Keep this around
auto sockAddr = socket.get();
BlockingSocket sock(std::move(socket));
SocketAddress addr;
addr.setFromHostPort(FLAGS_host, FLAGS_port);
sock.setAddress(addr);
sock.open();
LOG(INFO) << "connected to " << addr.getAddressStr();
sock.write((const uint8_t*)FLAGS_msg.data(), FLAGS_msg.size());
LOG(ERROR) << "TFO attempted: " << sockAddr->getTFOAttempted();
LOG(ERROR) << "TFO finished: " << sockAddr->getTFOFinished();
std::array<char, 1024> buf;
int32_t bytesRead = 0;
while ((bytesRead = sock.read((uint8_t*)buf.data(), buf.size())) != 0) {
std::cout << std::string(buf.data(), bytesRead);
}
sock.close();
return 0;
}
示例13: icmpSocket
int ICMPClient::ping(SocketAddress& address, int repeat) const
{
if (repeat <= 0) return 0;
ICMPSocket icmpSocket(_family);
SocketAddress returnAddress;
ICMPEventArgs eventArgs(address, repeat, icmpSocket.dataSize(), icmpSocket.ttl());
pingBegin.notify(this, eventArgs);
for (int i = 0; i < repeat; ++i)
{
icmpSocket.sendTo(address);
++eventArgs;
try
{
int t = icmpSocket.receiveFrom(returnAddress);
eventArgs.setReplyTime(i, t);
pingReply.notify(this, eventArgs);
}
catch (TimeoutException&)
{
std::ostringstream os;
os << address.host().toString() << ": Request timed out.";
eventArgs.setError(i, os.str());
pingError.notify(this, eventArgs);
continue;
}
catch (ICMPException& ex)
{
std::ostringstream os;
os << address.host().toString() << ": " << ex.what();
eventArgs.setError(i, os.str());
pingError.notify(this, eventArgs);
continue;
}
catch (Exception& ex)
{
std::ostringstream os;
os << ex.displayText();
eventArgs.setError(i, os.str());
pingError.notify(this, eventArgs);
continue;
}
}
pingEnd.notify(this, eventArgs);
return eventArgs.received();
}
示例14: bindAddr
int TCPSocket::bindAddr(SocketAddress &serveraddr)
{
if( m_iIpType == IPV4)
{
struct sockaddr_in addr;
memset(&addr,0,sizeof(addr));
addr.sin_family = PF_INET;
if(serveraddr.ifAnyAddr())
addr.sin_addr.s_addr = htons(INADDR_ANY);
else if((inet_aton(serveraddr.getIP(),(in_addr *)&addr.sin_addr.s_addr)) == 0)
{
handleSyscallError("TCPSocket::bindAddr");
return FAILED;
}
if(serveraddr.getPort()==0)
{
handleError("TCPSocket::getPort");
return FAILED;
}
addr.sin_port = htons(serveraddr.getPort());
if(bind(m_iSockFd,(const struct sockaddr *)&addr,sizeof(addr)) <0)
{
handleSyscallError("TCPSocket::bindAddr");
return FAILED;
}
} else
{
struct sockaddr_in6 addr;
memset(&addr,0,sizeof(addr));
addr.sin6_family = PF_INET6;
addr.sin6_scope_id = 2;
if( serveraddr.ifAnyAddr())
addr.sin6_addr = in6addr_any;
else
{
if( inet_pton(PF_INET6,serveraddr.getIP(),&(addr.sin6_addr)) < 0)
{
handleSyscallError("TCPSocket::bindAddr");
return FAILED;
}
}
if(serveraddr.getPort()==0)
{
handleError("TCPSocket::getPort");
return FAILED;
}
addr.sin6_port = htons(serveraddr.getPort());
if(bind(m_iSockFd,(const struct sockaddr *)&addr,sizeof(addr)) <0)
{
handleSyscallError("TCPSocket::bindAddr");
return FAILED;
}
}
return SUCCESSFUL;
}
示例15: listen
void ServerSocket::listen(const SocketAddress& local, int backlog,
bool reuseAddr, bool blocking) throw (IOException&)
{
close();
int sock = mxos::openSocket(AF_INET, SOCK_STREAM, 0);
if (-1 == sock)
{
THROW3(IOException, "Can't open socket", mxos::getLastSocketError());
}
if (reuseAddr)
{
int val = 1;
if (0 != mxos::setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val,
sizeof(val)))
{
mxos::closeSocket(sock);
THROW3(IOException, "Can't reuse addr", mxos::getLastSocketError());
}
}
if (0 != mxos::bind(sock, local.sockAddress(), local.length()))
{
mxos::closeSocket(sock);
THROW3(IOException, std::string("Can't bind socket to:") + local.toString(), mxos::getLastSocketError());
}
if (!blocking)
{
try
{
IOUtil::configBlocking(sock, false);
} catch (mxcore::IOException& e)
{
mxos::closeSocket(sock);
throw e;
}
}
if (0 != mxos::listen(sock, backlog))
{
mxos::closeSocket(sock);
THROW3(IOException, "Can't listen socket", mxos::getLastSocketError());
}
handle_ = sock;
}