Thrift obtains the Client ip address and thrift Client ip address through TServerEventHandler

Source: Internet
Author: User

Thrift obtains the Client ip address and thrift Client ip address through TServerEventHandler

After using thrift for so long, I have never known how to print the ip address of the sender at the receiving end. A stupid method is to add a field in struct and fill in the ip address (or host) of the sender. It is agreed that a valid ip address must be filled, but this not only increases the amount of code of the caller, the receiver also needs to determine whether a string field is a valid ip address.

I checked some information online some time ago, but it was fruitless. I reorganized these materials today and finally got it done.

General idea: Http://web.archiveorange.com/archive/v/fzey581cbguiblqh1gpv. I will repeat the following based on my ideas:

It mainly implements a class whose base class is TServerEventHandler. I call it ClientIPHandler.

1. Define a map <uint64, string> to store <pthread_self (), ip>.

2. Insert map in createContext () in ClientIPHandler and delete deleteContext.

3. Define a member function to obtain the map value. This function is used by your XServiceHandler (automatically generated by thrift.

4. Before calling serve (), define shared_ptr <ClientIPHandler> variable a and call setServerEventHandler to set eventHandler _ (server/TServer. h: 217 ).

The read and write operations of map must be locked.

Some code:

static map<uint64, std::string> thrift_client_ip;static base::lock::Mutex mutex;。。。class ClientIPHandler : virtual public TServerEventHandler { public:  ClientIPHandler() {  }  virtual ~ClientIPHandler() {  }  std::string GetThriftClientIp() {    lock::MutexLock g(&mutex);    return thrift_client_ip[pthread_self()];  }  virtual void preServe() {  }  virtual void* createContext(boost::shared_ptr<TProtocol> input,                              boost::shared_ptr<TProtocol> output) {    // insert when connection open    TBufferedTransport *tbuf = dynamic_cast<TBufferedTransport *>(input->getTransport().get());    TSocket *sock = dynamic_cast<TSocket *>(tbuf->getUnderlyingTransport().get());    lock::MutexLock g(&mutex);    thrift_client_ip[pthread_self()] = sock->getPeerAddress();    return NULL;  }  virtual void deleteContext(void* serverContext,                             boost::shared_ptr<TProtocol>input,                             boost::shared_ptr<TProtocol>output) {    // erase when connection close    lock::MutexLock g(&mutex);    thrift_client_ip.erase(pthread_self());  }  virtual void processContext(void* serverContext,                              boost::shared_ptr<TTransport> transport) {  } private:};


The corresponding namespaces are required for TServerEventHandler and TBufferedTransport. 

In my personal understanding, createContext is called when a connection is created, and deleteContext is called after the connection is disconnected.

Since all my code is written in a. h, getthrifle tclientip is not suitable for global functions.


So far, we can call getthrifle tclientip () in XServiceHandler to obtain the client's ip address, and finally no longer worry about where the data is sent.


Have you ever used the thrift framework? I want to get the client IP address from the java Server.

Obtain the client from the java Server
 
The ruijie client displays "An error occurred while obtaining IP information. Check whether the current configuration of the client meets the requirements of the network. After checking, try" the ultimate solution.

Fault occurrence 12: the client prompts "An error occurred while obtaining IP information. Please check whether the current configuration of the ruijie security agent meets the requirements of the network, after the check, try to authenticate again or prompt "the current configuration of ruijie does not meet the network requirements" Fault Cause: 1) the system functions have not been fully started, ruijie authentication automatically starts. 2) the IP address is not required by the school to dynamically obtain the Intranet address starting with 172. 3) computer system problems: for example, the DHCP client function is disabled. solution: 1) disconnect the ruijie authentication and re-authenticate after a short period of time. Do not check the four options in the software settings. 2) automatically obtain the IP Address: "Network Neighbor" right-click "properties"-> double-click "Local Connection"-> "properties"-> This connection uses the following items, and double-click "Internet Protocol (TCP/IP)" in the drop-down box) -> select "automatically obtain IP Address" and "automatically obtain DNS server address" 3) reinstall the NIC driver or the system; use exclusion to determine whether the problem is a personal system problem or a port range network problem. if this problem occurs on all computers on the port, submit a repair report to the O & M center.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.