本文整理汇总了C++中boost::asio::ip::tcp::socket类的典型用法代码示例。如果您正苦于以下问题:C++ socket类的具体用法?C++ socket怎么用?C++ socket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了socket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: send
void send()
{
BOOST_CHECK_EQUAL( socket_.is_open(), true );
size_t l = 5;
socket_.send( boost::asio::buffer( &l, sizeof(size_t) ) );
socket_.send( boost::asio::buffer( "hello", 5 ) );
}
示例2: if
void justine::sampleclient::ShmClient::gangsters ( boost::asio::ip::tcp::socket & socket, int id )
{
boost::system::error_code err;
size_t length = std::sprintf ( data, "<gangsters " );
length += std::sprintf ( data+length, "%d>", id );
socket.send ( boost::asio::buffer ( data, length ) );
length = socket.read_some ( boost::asio::buffer ( data ), err );
if ( err == boost::asio::error::eof )
{
// TODO
}
else if ( err )
{
throw boost::system::system_error ( err );
}
std::cout.write ( data, length );
std::cout << "Command GANGSTER sent." << std::endl;
}
示例3: DoConnect
//===========================================================================
void CHttpClient::DoConnect (
const boost::system::error_code & err,
boost::asio::ip::tcp::resolver::iterator iter
) {
if (err) {
if (iter != boost::asio::ip::tcp::resolver::iterator()) {
m_socket.close();
boost::asio::ip::tcp::endpoint endPoint = *iter;
auto handler = boost::bind(
&CHttpClient::DoConnect,
this,
boost::asio::placeholders::error,
++iter
);
m_socket.async_connect(endPoint, handler);
} else {
printf("%s\n", __FUNCTION__);
cout<<"Error: "<<err.message()<<endl;
}
return;
}
auto handler = boost::bind(
&CHttpClient::DoWriteRequest,
this,
boost::asio::placeholders::error
);
boost::asio::async_write(m_socket, m_request, handler);
}
示例4: on_connect
void on_connect(boost::asio::ip::tcp::socket &socket) {
SNOW_LOG_TRACE("new Connection socket fd {}, local addr {}, peer addr {}",
socket.native(),
socket.local_endpoint(),
socket.remote_endpoint());
std::make_shared<Connection>(socket, m_request_dispatcher, m_pkg_spliter, 100 * 1000)->start();
}
示例5: handleRead
/**
* Вызывается всякий раз, когда данные получены.
*/
static void handleRead(
ba::ip::tcp::socket& readFrom,
ba::ip::tcp::socket& writeTo,
char* readBuffer,
size_t bytes,
const boost::system::error_code& e
) {
#ifdef _DEBUG
const std::string data( readBuffer, readBuffer + bytes );
std::cout << data << std::endl;
#endif
// отправляем полученные данные "другой стороне"
writeTo.send( ba::buffer( readBuffer, bytes ) );
// читаем ещё данные с "этой стороны"
readFrom.async_read_some(
ba::buffer( readBuffer, 1024 ),
boost::bind(
handleRead,
boost::ref( readFrom ),
boost::ref( writeTo),
readBuffer,
ba::placeholders::bytes_transferred,
ba::placeholders::error
) );
}
示例6: validateSocket
/** Check that local endpoint is loopback
*
* @throws Face::Error if validation failed
*/
static void
validateSocket(boost::asio::ip::tcp::socket& socket)
{
if (!socket.local_endpoint().address().is_loopback() ||
!socket.remote_endpoint().address().is_loopback())
{
throw Face::Error("TcpLocalFace can be created only on loopback interface");
}
}
示例7: if
std::vector<justine::sampleclient::MyShmClient::Gangster> justine::sampleclient::MyShmClient::gangsters ( boost::asio::ip::tcp::socket & socket, int id,
osmium::unsigned_object_id_type cop )
{
boost::system::error_code err;
size_t length = std::sprintf ( data, "<gangsters " );
length += std::sprintf ( data+length, "%d>", id );
socket.send ( boost::asio::buffer ( data, length ) );
length = socket.read_some ( boost::asio::buffer ( data ), err );
if ( err == boost::asio::error::eof )
{
// TODO
}
else if ( err )
{
throw boost::system::system_error ( err );
}
/* reading all gangsters into a vector */
int idd {0};
unsigned f, t, s;
int n {0};
int nn {0};
std::vector<Gangster> gangsters;
while ( std::sscanf ( data+nn, "<OK %d %u %u %u>%n", &idd, &f, &t, &s, &n ) == 4 )
{
nn += n;
gangsters.push_back ( Gangster {idd, f, t, s} );
}
std::sort ( gangsters.begin(), gangsters.end(), [this, cop] ( Gangster x, Gangster y )
{
return dst ( cop, x.to ) < dst ( cop, y.to );
} );
std::cout.write ( data, length );
std::cout << "Command GANGSTER sent." << std::endl;
return gangsters;
}
示例8:
inline connection::~connection()
{
socket_m.close();
read_thread_m.join();
send_thread_m.join();
}
示例9: handle_request
void server::handle_request(boost::asio::ip::tcp::socket& socket,
const request& req, reply& rep)
{
using namespace boost::posix_time;
detail::s_log.reset(new ostringstream);
ptime start = second_clock::local_time();
try
{
handle_request(req, rep);
}
catch (...)
{
rep = reply::stock_reply(internal_server_error);
}
// protect the output stream from garbled log messages
boost::mutex::scoped_lock lock(detail::s_log_lock);
cout << socket.remote_endpoint().address()
<< " [" << start << "] "
<< second_clock::local_time() - start << ' '
<< rep.status << ' '
<< detail::s_log->str() << endl;
}
示例10: async_read
void handler::async_read() {
auto self(shared_from_this());
m_socket.async_read_some(boost::asio::buffer(m_buffer),
std::bind(&handler::handle_read, self,
std::placeholders::_1,
std::placeholders::_2));
}
示例11: GetRemoteAddressAndPort
bool TcpProxyServer::GetRemoteAddressAndPort(boost::asio::ip::tcp::socket& clientSocket, boost::asio::ip::address& remoteAddress, unsigned short& remotePort)
{
boost::asio::ip::tcp::socket::endpoint_type userAgentEnpoint = clientSocket.remote_endpoint();
unsigned short userAgentPort = userAgentEnpoint.port();
boost::asio::ip::address userAgentIP = userAgentEnpoint.address();
if(userAgentIP != boost::asio::ip::address_v4::from_string("127.0.0.1")) {
return false;
}
std::pair<u_long, USHORT> remoteAddressPair = WinsockHooker::GetRemoteAddressPair(userAgentPort);
if(remoteAddressPair.first == 0ul) {
return false;
}
boost::asio::ip::address_v4 remote_address(remoteAddressPair.first);
unsigned short remote_port = remoteAddressPair.second;
if(remote_address == boost::asio::ip::address_v4::from_string("127.0.0.1") && remote_port == listen_port)
{
return false;
}
remoteAddress = remote_address;
remotePort = remote_port;
TSINFO4CXX("Connect: IP:" << remoteAddress.to_string() << ", Port: " << remotePort);
return true;
}
示例12: start
void start()
{
socket_.async_read_some(boost::asio::buffer(data_),
boost::bind(&session::handle_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
示例13: connected
static void connected(const boost::system::error_code & ec, boost::asio::ip::tcp::socket & msocket)
{
if ( ec )
std::cout << ec.message() << std::endl;
else
msocket.write_some(boost::asio::buffer(std::string("GET / HTTP/1.1\r\n\r\n")));
}
示例14: start
inline void connection::start() {
socket_.async_read_some(boost::asio::buffer(buffer_),
strand_.wrap(
boost::bind(&connection::handle_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
示例15: handleConnect
bool CPanasonicNode::handleConnect(boost::asio::ip::tcp::socket& socket, boost::asio::ip::tcp::endpoint endpoint, boost::system::error_code& ec)
{
try
{
if (!m_stoprequested)
{
socket.connect(endpoint, ec);
if (!ec)
{
if (DEBUG_LOGGING) _log.Log(LOG_NORM, "Panasonic Plugin: (%s) Connected to '%s:%s'.", m_Name.c_str(), m_IP.c_str(), (m_Port[0] != '-' ? m_Port.c_str() : m_Port.substr(1).c_str()));
return true;
}
else
{
if (DEBUG_LOGGING)
if (((ec.value() != 113) && (ec.value() != 111) &&
(ec.value() != 10060) && (ec.value() != 10061) && (ec.value() != 10064) && (ec.value() != 10061))) // Connection failed due to no response, no route or active refusal
_log.Log(LOG_NORM, "Panasonic Plugin: (%s) Connect to '%s:%s' failed: (%d) %s", m_Name.c_str(), m_IP.c_str(), (m_Port[0] != '-' ? m_Port.c_str() : m_Port.substr(1).c_str()), ec.value(), ec.message().c_str());
return false;
}
}
}
catch (std::exception& e)
{
_log.Log(LOG_ERROR, "Panasonic Plugin: (%s) Exception: '%s' connecting to '%s'", m_Name.c_str(), e.what(), m_IP.c_str());
return false;
}
return true;
}