[英]Apache Mina SSHD 1.0 Server exits immediately
我正在使用 Apache Mina sshd-core-1.0.0 來啟動 SFTP 守護進程。 然而,程序在sshd.start()
之后退出。 沒有錯誤。 但是,如果我使用 sshd-core-0.0.14,服務器啟動就好了,我可以啟動 SFTP 會話。 我錯過了 1.0.0 的東西嗎?
帶有 1.0.0 的代碼片段(不起作用)
public static void main(String[] args) throws IOException {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(2222);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File ("hostkey.ser")))
sshd.setPasswordAuthenticator(new AuthenticatorImpl());
sshd.start();
}
帶有 0.0.14 的代碼片段(有效)
public static void main(String[] args) throws IOException {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(2222);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
sshd.setPasswordAuthenticator(new AuthenticatorImpl());
sshd.start();
}
以下是 1.0.0 運行時的輸出。 代碼開始正常,但在sshd.start()
語句之后終止。
2015-12-16 19:57:38,510 DEBUG SFTPServer.main(SFTPServer.java:26) message
2015-12-16 19:57:38,767 INFO org.apache.sshd.common.util.SecurityUtils$BouncyCastleRegistration.call(SecurityUtils.java:145) Trying to register BouncyCastle as a JCE provider
2015-12-16 19:57:39,076 INFO org.apache.sshd.common.util.SecurityUtils$BouncyCastleRegistration.call(SecurityUtils.java:149) Registration succeeded
2015-12-16 19:57:39,105 DEBUG org.apache.sshd.common.io.nio2.Nio2Acceptor.bind(Nio2Acceptor.java:57) Binding Nio2Acceptor to address 0.0.0.0/0.0.0.0:2222
2015-12-16 19:57:39,114 INFO SFTPServer.main(SFTPServer.java:32) Started
SshServer.Start
僅開始偵聽傳入端口。 它不會阻塞。 所以main
之后立即終止。 這在 0.0.14 中應該沒有任何不同,盡管我無法嘗試。
您必須在main
顯式等待以保持服務器運行。
查看SshServer.main
是如何實現的(在 0.0.14 和 1.0.0 中):
public static void main(String[] args) throws Exception {
...
SshServer sshd = SshServer.setUpDefaultServer();
...
sshd.start();
Thread.sleep(Long.MAX_VALUE);
}
我遇到了同樣的問題,這是因為 SSHD 不知道要使用哪個網絡庫。 我添加了 Netty 包:
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-netty</artifactId>
<version>2.6.0</version>
</dependency>
它在沒有 Thread.sleep() 調用的情況下工作。 在日志中,您將看到:
[main] INFO org.apache.sshd.common.io.DefaultIoServiceFactoryFactory - Using NettyIoServiceFactoryFactory
然后 Netty 開始響應:
[nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x7fa79050] REGISTERED
[nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x7fa79050] BIND: 0.0.0.0/0.0.0.0:2222
[nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler - [id: 0x7fa79050, L:/[0:0:0:0:0:0:0:0]:2222] ACTIVE
我沒有在 Maven 中添加任何其他 Netty 庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.