我正在尝试使用 JSCH 连接到远程服务器,然后从该服务器通过 tcp/ip 端口打开类似 telnet 的会话。说连接到服务器 A,一旦连接,就会通过另一个端口向服务器 B 发出 tcp 连接。在我的网络服务器日志中,我看到了一个 GET / 已记录但不是 GET /foo 正如我所期望的那样。我在这里想念什么?(我不需要使用端口转发,因为我连接的系统可以访问远程端口)
package com.tekmor;
import com.jcraft.jsch.*;
import java.io.BufferedReader;
.
.
public class Siranga {
public static void main(String[] args){
Siranga t=new Siranga();
try{
t.go();
} catch(Exception ex){
ex.printStackTrace();
}
}
public void go() throws Exception{
String host="hostXXX.com";
String user="USER";
String password="PASS";
int port=22;
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
String remoteHost="hostYYY.com";
int remotePort=80;
try {
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig(config);
session.connect();
Channel channel=session.openChannel("direct-tcpip");
((ChannelDirectTCPIP)channel).setHost(remoteHost);
((ChannelDirectTCPIP)channel).setPort(remotePort);
String cmd = "GET /foo";
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
channel.connect(10000);
byte[] bytes = cmd.getBytes();
InputStream is = new ByteArrayInputStream(cmd.getBytes("UTF-8"));
int numRead;
while ( (numRead = is.read(bytes) ) >= 0) {
out.write(bytes, 0, numRead);
System.out.println(numRead);
}
out.flush();
channel.disconnect();
session.disconnect();
System.out.println("foo");
}
catch (Exception e){
e.printStackTrace();
}
}
}