void StreamSocket::sendBuf(const ByteArray& buf) { size_t size = buf.size(); sendSize(size); os_.write(&buf[0], size); os_.flush(); }
void Slave::sendResult(int64_t id, vector<double> &resultArray, int64_t size) { // printf("Slave %d\n Sending...", rank); sendID(id); sendSize(size); sendArray(resultArray, size); // system("echo Slave: $(hostname -I)"); // printf("Slave %d Done.\n", rank); }
void StreamSocket::sendMsg(const std::string& msg) { size_t length = msg.length(); if (length > MAX_CONTROL_MESSAGE_SIZE) { throw std::string("length > MAX_CONTROL_MESSAGE_SIZE"); } sendSize(length); os_.write(msg.c_str(), length); os_.flush(); }
void Server::EnableAutodiscovery(std::string interface, std::string group, int port) { mMulticastSocket.reset(new udp::socket(mIOService)); mMulticastGroup = boost::asio::ip::address::from_string(group); boost::asio::ip::address localIp =boost::asio::ip::address::from_string(interface); mMulticastEndpoint = udp::endpoint(mMulticastGroup, port); mMulticastSocket->open(mMulticastEndpoint.protocol()); mMulticastSocket->set_option(udp::socket::reuse_address(true)); mMulticastSocket->bind(udp::endpoint(localIp, port)); mMulticastSocket->set_option(boost::asio::ip::multicast::join_group(mMulticastGroup.to_v4(), localIp.to_v4())); mMulticastSocket->set_option(boost::asio::ip::multicast::hops(5)); mMulticastSocket->set_option(boost::asio::ip::multicast::enable_loopback(true)); mMulticastSocket->set_option(boost::asio::socket_base::broadcast(true)); boost::asio::socket_base::receive_buffer_size receiveSize(100); mMulticastSocket->set_option(receiveSize); boost::asio::socket_base::send_buffer_size sendSize(100); mMulticastSocket->set_option(sendSize); std::stringstream ss; ss << interface << ":" << mPort << "END"; std::string data = ss.str(); mMulticastSocket->async_send_to( boost::asio::buffer(data), mMulticastEndpoint, boost::bind(&Server::HandleMulticastSend, this, boost::asio::placeholders::error, data)); }
AWE::uint32 TCP_Transfer_ClientThread::runThread() { // -->> Lock SocketTestCase::s_kSemaphore.aquire(); AWE::Socket::TCP::Protocol* pTCP = AWE::Socket::TCP::create(); CPPUNIT_ASSERT( pTCP ); // listen on our port AWE::Socket::Address kRemoteAddress; CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( pTCP->resolveAddress( &kRemoteAddress, (SocketTestCase::s_bTestIP4 ? SocketTestCase::s_iServIP_v4 : SocketTestCase::s_iServIP_v6), g_iServerPort//, // (SocketTestCase::s_bTestIP4 ? AF_INET : AF_INET6) ) ) == AWE::Socket::Error::Success ); AWE::Socket::Address kLocalAddress; CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( pTCP->resolveLocalAddress( &kLocalAddress, 0, (SocketTestCase::s_bTestIP4 ? AF_INET : AF_INET6) ) ) == AWE::Socket::Error::Success ); CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( pTCP->createSocket( kRemoteAddress.getAddressFamily() ) ) == AWE::Socket::Error::Success ); // Bind AWE::Socket::StreamProtocolHost< AWE::Socket::TCP::Protocol > kHost( pTCP ); CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( kHost.bind( kLocalAddress ) ) == AWE::Socket::Error::Success ); CPPUNIT_ASSERT( AWE::Socket::Error::convertNativeError( pTCP->getBoundAddress( &kLocalAddress ) ) == AWE::Socket::Error::Success ); // Connect AWE::Core::String sAddress; AWE::uint16 iPort( 0 ); kRemoteAddress.getAddress( &sAddress ); kRemoteAddress.getPort( &iPort ); std::cout << "[Client] Connecting to \"" << sAddress << "\"" << " port " << iPort << std::endl << std::flush; AWE::Socket::StreamProtocolClient< AWE::Socket::TCP::Protocol > kClient( pTCP ); // as we are using blocking sockets, there is a small // race window between the server releasing the semaphore // and actually calling listen on the socket // this is indicated by the connect returning CONNREFUSED // this is not an issue with the socket library but a deficiency // of this test AWE::Socket::Error::_t eError; do { eError = AWE::Socket::Error::convertNativeError( kClient.connect( kRemoteAddress ) ); } while ( eError == AWE::Socket::Error::ConnectionRefused ); CPPUNIT_ASSERT( eError == AWE::Socket::Error::Success ); pTCP->getBoundAddress( &kLocalAddress ); kLocalAddress.getAddress( &sAddress ); kLocalAddress.getPort( &iPort ); std::cout << "[Client] Bound to \"" << sAddress << "\"" << " port " << iPort << std::endl << std::flush; // transfer some text AWE::uint32 sendSize( 0 ); // send isn't guaranteed to send the entire buffer // so we must loop while ( sendSize != g_sText.length() ) { int iResult = pTCP->send( g_sText.c_str() + sendSize, (AWE::uint32)g_sText.length() - sendSize ); if ( iResult > 0 ) { sendSize += iResult; } } std::cout << "[Client] Sent \"" << g_sText << "\"" << std::endl << std::flush; CPPUNIT_ASSERT( sendSize == g_sText.length() ); #if defined( WAIT_BEFORE_DESTROY ) // -->> Lock SocketTestCase::s_kSemaphore.aquire(); #endif pTCP->closeSocket(); AWE_SAFE_DELETE( pTCP ); return 0; }