Revision 1 as of 2012-08-10 02:58:57

Clear message
Locked History Actions

thrift/remoteIP

リモートIPアドレスの取得

サーバ側に接続してきたクライアントのIPアドレスを取得する方法の一つ(他にもあるかもしれない)。 ここではTThreadPoolServerを使用するものとし、それを改造する。 thrift-0.8.0の場合

  • TThreadPoolServerのソースをまるまるコピーし、名前をMyThreadPoolServerとでもする。

  • (どこでも良いのだが)MyThreadPoolServerのstaticフィールドとして以下を定義。

public static ThreadLocal<String> remoteIpAddress = new ThreadLocal<String>();
  • 内部クラスWorkerProcessのrun()メソッドの冒頭に以下を追加。

      try {
        Field field = TSocket.class.getDeclaredField("socket_");
        field.setAccessible(true);
        Socket socket = (Socket)field.get(client_);
        String ip = socket.getInetAddress().getHostAddress();
        remoteIpAddress.set(ip);        
      } catch (Exception ex) {
        ex.printStackTrace();
      }
  • ハンドラの中では以下のようにipアドレスを取得。

  String remoteIp = MyThreadPoolServer.remoteIpAddress.get();