SocketFactoryIns socketFactory = new SocketFactoryIns();
socketFactory.setLocalAddressIp(bindAddress);
session.setSocketFactory(socketFactory);
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import com.jcraft.jsch.SocketFactory;
import com.ztesoft.zsmart.uip.base.util.LogBase;
public class SocketFactoryIns implements SocketFactory {
private LogBase iplog = LogBase.getLogger(SocketFactoryIns.class);
private InputStream in = null;
private OutputStream out = null;
private String localAddressIp = null;
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
// TODO Auto-generated method stub
InetAddress remoteIp = InetAddress.getByName(host);
InetAddress localIp = InetAddress.getByName(localAddressIp);
iplog.info("remoteIp >>" + remoteIp.toString());
iplog.info("localIp >>" + localIp.toString());
Socket socket = new Socket(remoteIp, 22, localIp, 0);
iplog.info("socket created >> " + socket.toString());
return socket;
}
public String getLocalAddressIp() {
return localAddressIp;
}
public void setLocalAddressIp(String localAddressIp) {
this.localAddressIp = localAddressIp;
}
@Override
public InputStream getInputStream(Socket socket) throws IOException {
// TODO Auto-generated method stub
if (in == null)
in = socket.getInputStream();
return in;
}
@Override
public OutputStream getOutputStream(Socket socket) throws IOException {
// TODO Auto-generated method stub
if (out == null)
out = socket.getOutputStream();
return out;
}
public static void main(String[] args){
SocketFactoryIns ins = new SocketFactoryIns();
try {
ins.createSocket("10.45.50.112", 22);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}