本文整理汇总了C++中boost::asio::ip::tcp::socket类的典型用法代码示例。如果您正苦于以下问题:C++ socket类的具体用法?C++ socket怎么用?C++ socket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了socket类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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 ) );
}
开发者ID:nick0lay,项目名称:cpp_craft_1013,代码行数:7,代码来源:async_tcp_client_tests.cpp
示例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;
}
开发者ID:kisfiu,项目名称:Tigers,代码行数:28,代码来源:shmclient.cpp
示例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);
}
开发者ID:hellojaewon,项目名称:study,代码行数:30,代码来源:HttpClient.cpp
示例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();
}
开发者ID:weijingtao,项目名称:snow,代码行数:7,代码来源:proxy.hpp
示例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
) );
}
开发者ID:signmotion,项目名称:server,代码行数:30,代码来源:main.cpp
示例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");
}
}
开发者ID:PhilipsIII,项目名称:NFD,代码行数:13,代码来源:tcp-face.hpp
示例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;
}
开发者ID:vellindra,项目名称:Robocar-Debrecen4-Final,代码行数:48,代码来源:myshmclient.cpp
示例8:
inline connection::~connection()
{
socket_m.close();
read_thread_m.join();
send_thread_m.join();
}
开发者ID:Palmik,项目名称:Boost.IRC,代码行数:7,代码来源:connection.hpp
示例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;
}
开发者ID:BackupTheBerlios,项目名称:libzeep-svn,代码行数:25,代码来源:http-server.cpp
示例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));
}
开发者ID:3Hren,项目名称:elliptics,代码行数:7,代码来源:server.cpp
示例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;
}
开发者ID:yuanbaoyuele,项目名称:yuanbaoyule,代码行数:27,代码来源:TcpProxyServer.cpp
示例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));
}
开发者ID:mpapierski,项目名称:simpledaemon,代码行数:7,代码来源:main.cpp
示例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")));
}
开发者ID:AimuTran,项目名称:avbot,代码行数:7,代码来源:multi_proxychain.cpp
示例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)));
}
开发者ID:RubenVerborgh,项目名称:SWObjects,代码行数:7,代码来源:WEBserver_asio.hpp
示例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;
}
开发者ID:jonlar,项目名称:domoticz,代码行数:31,代码来源:PanasonicTV.cpp
示例16: setupSocket
void TCPClient::setupSocket(boost::asio::ip::tcp::socket& socket)
{
if (!_setup_socket)
{
_setup_socket = true;
socket.set_option(boost::asio::ip::tcp::no_delay(true));
}
}
开发者ID:wangfeilong321,项目名称:MuseOpenIG,代码行数:8,代码来源:tcpclient.cpp
示例17: assert
bool server::read_socket_from_parent(int fd_socket, boost::asio::ip::tcp::socket& socket)
{
typedef boost::asio::ip::tcp::socket::native_type native_type;
#if __APPLE__
// macos is special...
assert(CMSG_SPACE(sizeof(int)) == 16);
#endif
struct msghdr msg;
union {
struct cmsghdr cm;
#if __APPLE__
char control[16];
#else
char control[CMSG_SPACE(sizeof(int))];
#endif
} control_un;
msg.msg_control = control_un.control;
msg.msg_controllen = sizeof(control_un.control);
msg.msg_name = NULL;
msg.msg_namelen = 0;
boost::asio::ip::tcp::socket::endpoint_type peer_endpoint;
struct iovec iov[1];
iov[0].iov_base = peer_endpoint.data();
iov[0].iov_len = peer_endpoint.capacity();
msg.msg_iov = iov;
msg.msg_iovlen = 1;
bool result = false;
int n = recvmsg(fd_socket, &msg, 0);
if (n >= 0)
{
peer_endpoint.resize(n);
struct cmsghdr* cmptr CMSG_FIRSTHDR(&msg);
if (cmptr != NULL and cmptr->cmsg_len == CMSG_LEN(sizeof(int)))
{
if (cmptr->cmsg_level != SOL_SOCKET)
cerr << "control level != SOL_SOCKET" << endl;
else if (cmptr->cmsg_type != SCM_RIGHTS)
cerr << "control type != SCM_RIGHTS";
else
{
int fd = *(reinterpret_cast<native_type*>(CMSG_DATA(cmptr)));
if (fd >= 0)
{
socket.assign(peer_endpoint.protocol(), fd);
result = true;
}
}
}
else
cerr << "No file descriptor was passed" << endl;
}
开发者ID:BackupTheBerlios,项目名称:libzeep-svn,代码行数:58,代码来源:http-server.cpp
示例18: do_write
void do_write(boost::asio::ip::tcp::socket& socket, boost::asio::yield_context yield)
{
using namespace boost::asio::ip;
std::time_t now = std::time(0);
std::string data = std::ctime(&now);
boost::asio::async_write(socket, boost::asio::buffer(data), yield);
socket.shutdown(tcp::socket::shutdown_send);
}
开发者ID:uwydoc,项目名称:the-practices,代码行数:9,代码来源:asio_time_server_coroutine.cpp
示例19: run_socket_io
void run_socket_io(boost::asio::ip::tcp::socket& socket)
{
std::array<char, 1024> read_buffer;
std::array<char, 1024> write_buffer;
socket.async_read_some(boost::asio::buffer(write_buffer, 1024),
[&write_buffer](const boost::system::error_code& ec, std::size_t length)
{
if (!ec)
std::cout.write(write_buffer.data(), length);
});
while (std::cin.getline(read_buffer.data(), 1024))
{
socket.send(
boost::asio::buffer(read_buffer, std::strlen(read_buffer.data())));
}
}
开发者ID:Surogate,项目名称:Utilities,代码行数:18,代码来源:main.cpp
示例20: send
virtual void send(raw_data& data) {
bool write_in_progress = !buffer_write_queue_.empty();
buffer_write_queue_.push_back(data);
if (!write_in_progress) {
socket_.async_write_some(
boost::asio::buffer(buffer_write_queue_.front().bytes, buffer_write_queue_.front().length),
boost::bind(&session::handle_write, shared_from_this(),
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
}
开发者ID:Neoracle,项目名称:socket-session-retainer,代码行数:10,代码来源:session.hpp
注:本文中的boost::asio::ip::tcp::socket类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论