2013-07-30 61 views
0

我連接到一個軸2服務器通過存根一個Axis2服務器客戶端綁定的IP地址:獲取與多網卡

MyServiceStub fcc = new MyServiceStub(url); 

一旦連接建立,因爲我有上配置不同的IP地址該機器在不同的子網上,我想知道什麼是與服務器連接的地址。

例如,如果我有一個2個網卡和IP地址,這樣配置的機器:

eth0 ---> 192.165.0.1 
eth1 ---> 10.0.3.1 

,我對IP的Axis服務器:10.0.3.33,一旦連接建立,如何我可以知道有界的IP地址嗎?

如何獲取連接的客戶端IP地址(本例中爲10.0.3.1)?

+0

put in answ呃,但我不知道在100% – Deckard27

回答

0

也許你可以,如果你正在使用https協議來調用Axis2 Web服務嘗試這樣的事情

request.getHeader("url"); 
String ipAddress = request.getHeader("X-FORWARDED-FOR"); 
    if (ipAddress == null) { 
     ipAddress = request.getRemoteAddr(); 
    } 
0

,你可以實現你自己的SecureProtocolSocketFactory是這樣的:在某種程度上

public class CustomProtocolSocketFactory implements SecureProtocolSocketFactory { 
    private static final Logger logger = Logger.getLogger("trace"); 
    InetAddress localAddress=null; 
    SSLContext ctx=null; 
    Socket socket=null; 
    public CustomProtocolSocketFactory(SSLContext ctx) { 
     this.ctx = ctx; 
    } 
    public CustomProtocolSocketFactory(SSLContext ctx, InetAddress localAddress) { 
     this.ctx = ctx; 
     this.localAddress=localAddress; 
    } 
    public CustomProtocolSocketFactory(SSLContext ctx, InetAddress localAddress, Socket socket) { 
     this.ctx = ctx; 
     this.localAddress=localAddress; 
     this.socket=socket; 
    } 
    @Override 
    public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) 
      throws IOException, UnknownHostException { 
     logger.debug("createSocket(String host, int port, InetAddress localAddress, int localPort)"); 
     return ctx.getSocketFactory().createSocket(host, port, localAddress, localPort); 
    } 
    @Override 
    public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, 
      HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { 
     logger.debug("createSocket(String host, int port, InetAddress localAddress, int localPort,HttpConnectionParams params)"); 
     if (params == null) { 
      throw new IllegalArgumentException("Parameters may not be null"); 
     } 
     int timeout = params.getConnectionTimeout(); 
     SocketFactory socketfactory = ctx.getSocketFactory(); 
     if (timeout == 0) { 
      if(this.localAddress!=null){ 
       return socketfactory.createSocket(host, port, this.localAddress, localPort); 
      } 
      else{ 
       return socketfactory.createSocket(host, port, localAddress, localPort); 
      } 
     } else { 
      Socket socket = socketfactory.createSocket(); 
      if(this.localAddress!=null){ 
       SocketAddress localaddr = new InetSocketAddress(this.localAddress, localPort); 
       SocketAddress remoteaddr = new InetSocketAddress(host, port); 
       socket.bind(localaddr); 
       socket.connect(remoteaddr, timeout); 
       return socket; 
      } 
      else{ 
       SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); 
       SocketAddress remoteaddr = new InetSocketAddress(host, port); 
       socket.bind(localaddr); 
       socket.connect(remoteaddr, timeout); 
       return socket; 
      } 
     } 
    } 
    @Override 
    public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 
     if(localAddress!=null){ 
      logger.debug("createSocket(String host, int port) localAddress not null"); 
      return createSocket(host, port, localAddress, 0); 
     } 
     else{ 
      logger.debug("createSocket(String host, int port)"); 
      return ctx.getSocketFactory().createSocket(host, port); 
     } 
    } 
    @Override 
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) 
      throws IOException, UnknownHostException { 
     logger.debug("createSocket(Socket socket, String host, int port, boolean autoClose)"); 
     return ctx.getSocketFactory().createSocket(socket, host, port, autoClose); 
    } 
} 

那你可以使用InetAddress localAddress綁定套接字並使用像這樣:

proxy._getServiceClient().getOptions().setProperty(HTTPConstants.CUSTOM_PROTOCOL_HANDLER, 
          new Protocol("https",(ProtocolSocketFactory)new CustomProtocolSocketFactory(context,localAddress),port)); 
+0

如何提取連接的IP地址?問題是:我有兩個IP地址,我想知道哪一個連接到軸網絡服務器 – aleroot