|
26 | 26 |
|
27 | 27 | package com.jcraft.jsch;
|
28 | 28 |
|
29 |
| -import java.net.Socket; |
30 | 29 | import java.io.File;
|
31 | 30 | import java.io.FileInputStream;
|
32 | 31 | import java.io.InputStream;
|
33 | 32 | import java.io.IOException;
|
| 33 | +import java.net.InetSocketAddress; |
| 34 | +import java.net.Socket; |
| 35 | +import java.net.SocketTimeoutException; |
34 | 36 | import java.nio.charset.Charset;
|
35 | 37 | import java.nio.charset.StandardCharsets;
|
36 | 38 | import java.util.Vector;
|
@@ -369,55 +371,20 @@ static boolean array_equals(byte[] foo, byte bar[]) {
|
369 | 371 | }
|
370 | 372 |
|
371 | 373 | static Socket createSocket(String host, int port, int timeout) throws JSchException {
|
372 |
| - Socket socket = null; |
373 |
| - if (timeout == 0) { |
374 |
| - try { |
375 |
| - socket = new Socket(host, port); |
376 |
| - return socket; |
377 |
| - } catch (Exception e) { |
378 |
| - String message = e.toString(); |
379 |
| - throw new JSchException(message, e); |
380 |
| - } |
381 |
| - } |
382 |
| - final String _host = host; |
383 |
| - final int _port = port; |
384 |
| - final Socket[] sockp = new Socket[1]; |
385 |
| - final Exception[] ee = new Exception[1]; |
386 |
| - String message = ""; |
387 |
| - Thread tmp = new Thread(() -> { |
388 |
| - sockp[0] = null; |
389 |
| - try { |
390 |
| - sockp[0] = new Socket(_host, _port); |
391 |
| - } catch (Exception e) { |
392 |
| - ee[0] = e; |
393 |
| - if (sockp[0] != null && sockp[0].isConnected()) { |
394 |
| - try { |
395 |
| - sockp[0].close(); |
396 |
| - } catch (Exception eee) { |
397 |
| - } |
398 |
| - } |
399 |
| - sockp[0] = null; |
400 |
| - } |
401 |
| - }); |
402 |
| - tmp.setName("Opening Socket " + host); |
403 |
| - tmp.start(); |
| 374 | + Socket socket = new Socket(); |
404 | 375 | try {
|
405 |
| - tmp.join(timeout); |
406 |
| - message = "timeout: "; |
407 |
| - } catch (InterruptedException eee) { |
408 |
| - } |
409 |
| - if (sockp[0] != null && sockp[0].isConnected()) { |
410 |
| - socket = sockp[0]; |
411 |
| - } else { |
412 |
| - message += "socket is not established"; |
413 |
| - if (ee[0] != null) { |
414 |
| - message = ee[0].toString(); |
| 376 | + socket.connect(new InetSocketAddress(host, port), timeout); |
| 377 | + return socket; |
| 378 | + } catch (Exception e) { |
| 379 | + try { |
| 380 | + socket.close(); |
| 381 | + } catch (Exception ignore) { |
415 | 382 | }
|
416 |
| - tmp.interrupt(); |
417 |
| - tmp = null; |
418 |
| - throw new JSchException(message, ee[0]); |
| 383 | + |
| 384 | + String message = |
| 385 | + e instanceof SocketTimeoutException ? "timeout: socket is not established" : e.toString(); |
| 386 | + throw new JSchException(message, e); |
419 | 387 | }
|
420 |
| - return socket; |
421 | 388 | }
|
422 | 389 |
|
423 | 390 | static byte[] str2byte(String str, Charset encoding) {
|
|
0 commit comments