问题描述
我正在考虑一个JSch库用法的示例,可以在这里找到它:
http://www.jcraft.com/jsch/examples/ScpFrom.java.html
I am contemplating over an example of JSch library usage which can be found here:
http://www.jcraft.com/jsch/examples/ScpFrom.java.html
我无法从此示例理解几种代码模式.他们在这里:
I cannot understand several code patterns from this example. Here they are:
-
是否有任何理由更喜欢SCP而不是可以使用同一库运行的SFTP?
Is there any reasons to prefer SCP over SFTP, which can be ran using the same library?
为什么我们在远程主机上运行scp -f <remote file>
而不是仅仅运行scp source_file_path destination_file_path
?为什么在远程主机上执行效果更好?
Why we run scp -f <remote file>
on remote host instead of running simply scp source_file_path destination_file_path
? Why execution on remote host is better?
在传输开始时有一行
while(true){
int c=checkAck(in);
if(c!='C'){
break;
}
...
这个神奇的C
字母是什么意思?为什么选择C
?
what is the meaning of this magical C
letter? Why C
?
为什么总是一直发送该信号?
Why to send this signal all the time?
// send '\0'
buf[0]=0; out.write(buf, 0, 1); out.flush();
这如何读取文件大小?
how this can read filesize?
long filesize=0L;
while(true){
if(in.read(buf, 0, 1)<0){
// error
break;
}
if(buf[0]==' ')break;
filesize=filesize*10L+(long)(buf[0]-'0'); //What is this??
}
推荐答案
- 是否有任何理由更喜欢SCP而不是可以使用同一库运行的SFTP?
否,SCP协议已过时.您现在应该始终使用SFTP.
No, the SCP protocol is obsolete. You should always use SFTP nowadays.
尽管SFTP协议的琐碎实现往往比SCP传输慢(由于其数据包性质,要有效地实现SFTP传输并不容易).
- 为什么我们在远程主机上运行
scp -f <remote file>
而不是简单地运行scp source_file_path destination_file_path
?为什么在远程主机上执行更好?
- Why we run
scp -f <remote file>
on remote host instead of running simplyscp source_file_path destination_file_path
? Why execution on remote host is better?
SCP协议的工作方式. OpenSSH scp
二进制文件既可以用作服务器,也可以用作客户端.因此,当您在本地运行scp
时,它将通过SSH连接并在服务器上运行scp
.然后,这两个实例相互通信. JSch替换scp
的本地实例.但是它仍然需要远程实例来完成传输.
It's how SCP protocol works. The OpenSSH scp
binary works both as a server and a client. So when you run scp
locally, it connects over SSH and runs scp
on the server. Then the two instances communicate with each other. The JSch replaces the local instance of the scp
. But it still needs the remote instance to complete the transfer.
如果在本地运行scp
,则必须在计算机上安装OpenSSH.在Unix上可能很常见,但在Windows上绝对不常见.同样,没有一种简单/标准化的方法可以从scp
程序中捕获结果,并将其转换为JSch Java接口.这是JSch(或任何其他SFTP库)自行实现SFTP协议而不使用sftp
程序的同一原因.
If you were running scp
locally, you would have to have OpenSSH installed on the machine. What is maybe common on Unix, but definitely not on Windows. Also there's no easy/standardized way to capture results from the scp
program, to translate them to JSch Java interface. It's the same reason why JSch (or any other SFTP library) implements SFTP protocol on its own, instead of using sftp
program.
- 这个神奇的C字母是什么意思?为什么选择C?
SCP协议的每个命令都由一个字符标识. C
代表文件传输",D
代表目录传输",T
在文件传输指示下一个传输文件的修改时间之前,等等.我不知道为什么它是C
而不是例如F
.
Every command of the SCP protocol is identified by a single character. The C
stands for "file transfer", D
stands for "directory transfer", T
before file transfer indicates modification time of the next transferred file, etc. I do not know why it's C
and not for example F
.
- 为什么总是一直发送该信号?
NULL(\0
)字符命令是对另一个站点的确认/响应,该站点已完成接收到的命令.
The NULL (\0
) character command is confirmation/response to the other site that the received command was completed.
- 这如何读取文件大小?
C
命令具有语法(这是人类可读的字符串):
The C
command has syntax (it's a human-readable string):
C permissions size filename
例如:
C 0644 153634 index.php
代码中的循环将字符串"153634"
转换为数字153634.看起来实现起来有点太复杂了,但是可以用.
The loop in the code translates the string "153634"
to a number 153634. It looks like a bit too complicated implementation, but it works.
另请参见 SCP(安全复制协议)文件传输如何工作?
这篇关于JSch库中SCP协议实现的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!