在localhost移植时的jsch connectionerror PortforwardingL

时间:2014-01-23 10:38:08

标签: jdbc jsch portforwarding

我在建立jsch连接时遇到了一些麻烦。

com.jcraft.jsch.JSchException:PortForwardingL:本地端口127.0.0.1:1234不能是bound.com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

这是我的代码:

String user = "datafromcli";
        String password = "n5gbz7f";
        String host = "192.168.0.195";    // These are the details of my host/server in order for 
        int port=22;                      // the javaprogram to connect with ssh IP-user
        try
            {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            lport = 1234;                 // This is an outgoing port from my client
            rhost = "192.168.0.195";      // this is yet again the host/server
            rport = 3306;                 // This is the incomming port of my host/server
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            System.out.println("Establishing Connection...");
            session.connect();
            int assinged_port=session.setPortForwardingL(lport, rhost, rport);
            System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport);
            }
        catch(Exception e){System.err.print(e);}
    }
    public static void main(String[] args) {
        try{
            go();
        } catch(Exception ex){
            ex.printStackTrace();
        }
          System.out.println("An example for updating a Row from Mysql Database!");
          Connection con = null;
          String driver = "com.mysql.jdbc.Driver";       // (note) 
          String url = "jdbc:mysql://" + rhost +":" + lport + "/";   // (note2)
          String db = "tradb";                             // the name of the db in mysql
          String dbUser = "test1";         // the user in mysql
          String dbPasswd = "huuge242";  //the pass in mysql
          try{
          Class.forName(driver);
          con = (Connection) DriverManager.getConnection(url+db, dbUser, dbPasswd);
          try{

(注意)我一直试图找到所谓的驱动程序(在jsch和jdbc手册页中但没有运气,我原以为jsch或jdbc .jar文件中可能存在信息,但我还没有反编译它们还没看到。

(注2)我不确定是jdbc:mysql://是地址的前缀还是其他网址的符号。

我在路由器/交换机上打开了端口22(用于ssh到服务器),1234(用于客户端)和3306(用于服务器的mysql)。服务器通过5端口路由器/交换机连接到客户端,通常ssh和ftp手动工作,都使用filezilla和终端。

我已经发表了//评论,以解释我是如何理解代码行的。也许我解释错了。

此致

2 个答案:

答案 0 :(得分:0)

你必须将boundaddress添加为0.0.0.0, 尝试修改您的代码,如下所示,然后检查

String boundaddress ="0.0.0.0";
int assinged_port=session.setPortForwardingL(boundaddress,lport, rhost, rport);

答案 1 :(得分:0)

我认为这是因为用户没有绑定地址的权限。