2014-01-23 35 views
1

我在建立jsch連接時遇到了一些麻煩。jsch connectionerror本地主機移植PortforwardingL

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文件imformation但還沒t反編譯它們還沒有看到。

(note 2)我不知道的是jdbc:mysql://是地址的前綴或另一個url的符號。

我一直在我的路由器/交換機上打開端口22(用於SSH到服務器),1234(用於客戶端)和3306(用於服務器的MySQL)。服務器通過5端口路由器/交換機連接到客戶端,通常ssh和ftp都可以手動工作,包括filezilla和終端。

我做了//評論,以解釋我如何理解代碼行。也許我把它解釋錯了。

真誠

回答

0

您必須添加boundaddress爲0.0.0.0, 嘗試如下修改代碼和檢查

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

我相信這是因爲用戶沒有權限綁定的地址。