如何获取C++节俭的客户端 IP 地址
How to get client IP Address with C++ in thrift
我目前正在C++中实现基于节俭的(0.4.0)服务,并遇到了一个问题:
有没有办法从服务方法实现内部获取客户端的 IP 地址?我正在使用 TNonblocking服务器。
提前感谢!
在 TNonblockingServer 中,当 TProcessor::p rocess() 被调用时,TProtocol.transport 是一个 TMemoryBuffer,因此获取客户端 IP 地址是不可能的。
但是我们可以扩展类 TServerEventHandler,方法TServerEventHandler::p rocessContext() 在客户端即将调用处理器时调用。
static boost::thread_specific_ptr<std::string> thrift_client_ip; // thread specific
class MyServerEventHandler : public TServerEventHandler
{
virtual void processContext(void* serverContext, boost::shared_ptr<TTransport> transport)
{
TSocket *sock = static_cast<TSocket *>(transport.get());
if (sock)
{
//thrift_client_ip.reset(new string(sock->getPeerAddress())); // 0.9.2, reused TNonblockingServer::TConnection return dirty address, see https://issues.apache.org/jira/browse/THRIFT-3270
sock->getCachedAddress(); // use this api instead
}
}
};
// create nonblocking server
TNonblockingServer server(processor, protocolFactory, port, threadManager);
boost::shared_ptr<MyServerEventHandler> eventHandler(new MyServerEventHandler());
server.setServerEventHandler(eventHandler);
Ticket THRIFT-1053描述了对Java的类似请求。解决方案基本上是允许访问内部(端点)传输并从中检索数据。如果没有真正测试,为C++构建类似的解决方案应该很容易。由于您使用的是 Thrift 0.4.0,因此我强烈建议您先查看当前的主干 (0.9.3)。TBufferedTransport
、TFramedTransport
和TShortReadTransport
已经实施
boost::shared_ptr<TTransport> getUnderlyingTransport();
因此,上面提到的补丁可能根本不需要。
调用 TProcessor
派生类时process()
获取这两个传输。如果覆盖该方法,您应该能够管理对感兴趣的数据的访问:
/**
* A processor is a generic object that acts upon two streams of data, one
* an input and the other an output. The definition of this object is loose,
* though the typical case is for some sort of server that either generates
* responses to an input stream or forwards data from one pipe onto another.
*
*/
class TProcessor {
public:
// more code
virtual bool process(boost::shared_ptr<protocol::TProtocol> in,
boost::shared_ptr<protocol::TProtocol> out,
void* connectionContext) = 0;
// more code
#ifndef NONBLOCK_SERVER_EVENT_HANDLER_H
#define NONBLOCK_SERVER_EVENT_HANDLER_H
#include <thrift/transport/TSocket.h>
#include <thrift/server/TServer.h>
namespace apache{
namespace thrift{
namespace server{
class ServerEventHandler:public TServerEventHandler{
void* createContext(boost::shared_ptr<TProtocol> input, boost::shared_ptr<TProtocol> output){
(void)input;
(void)output;
return (void*)(new char[32]);//TODO
}
virtual void deleteContext(void* serverContext,
boost::shared_ptr<TProtocol>input,
boost::shared_ptr<TProtocol>output) {
delete [](char*)serverContext;
}
virtual void processContext(void *serverContext, boost::shared_ptr<TTransport> transport){
TSocket *tsocket = static_cast<TSocket*>(transport.get());
if(socket){
struct sockaddr* addrPtr;
socklen_t addrLen;
addrPtr = tsocket->getCachedAddress(&addrLen);
if (addrPtr){
getnameinfo((sockaddr*)addrPtr,addrLen,(char*)serverContext,32,NULL,0,0) ;
}
}
}
};
}
}
}
#endif
boost::shared_ptr<ServerEventHandler> serverEventHandler(new ServerEventHandler()
server.setServerEventHandler(serverEventHandler);
相关文章:
- 我可以与 python 服务器而不是 c++ 客户端建立 tcp/ip 套接字吗?
- C++ Winsock2 客户端未通过远程 IP 连接到服务器
- PHP Profiler语言 - 使用 C++ 从 PHP 扩展获取客户端 IP 和 URL
- 如何将请求的客户端连接的 IP 与 QTcpSocket 类中识别的 IP 之一进行比较?
- gRPC C++尝试在无法访问的 IP 上连接通道时阻止客户端
- UDP客户端/服务器程序在不同的IP和端口上通信超过2个插座
- TCP/IP 客户端是否可以连接到无法访问的 IP
- 服务器和客户端都在PC上运行.客户端可以使用PC的IP连接到服务器
- boost::asio::ip::tcp::iostream,先启动客户端,然后等待服务器
- 限制IP C 的客户端数量
- 提升::ASIO 获取客户端 IP
- C++客户端到Java服务器(TCP/IP)
- 一般来说,如何找出客户端是否分配了静态Ip或是dhcp客户端
- 提升连接后从TCP服务器访问TCP客户端IP地址+端口
- 如何获取C++节俭的客户端 IP 地址
- 如何在接受C++中的连接之前获取客户端 IP 地址
- 如何从 http 请求中获取客户端 IP
- 如何在 C++ 中获取 rdp 客户端 ip
- 如何使用libssh获取客户端ip地址
- 如何使用c++驱动程序连接两个客户端IP到相同的mongodb