You are absolute correct that ‘socket’ can never be null. It was my mistake. After further testing and investigation, I found that instead it should include an additional ‘catch’ statement as below. This is to disallow throwing the SmackException upstream as this will cause the application to abort file transfer process. The SmackException was thrown when res = false:
catch (SmackException e) {
incrementConnectionFailures(address);
}
Below is the proposed final code implementation to resolve the problem that I have experienced.
Attached below also contain the log messages for the types of Exceptions that may occur when socket = socks5Client.getSocket(timeout); is being executed. The log message is captured using a modified code with log methods (attached below).
Sorry for the earlier confusion created.
===================== Final proposed code implementation ==========
public Socks5BytestreamSession accept() throws InterruptedException, XMPPErrorException, SmackException
{
Collection<StreamHost> streamHosts = this.bytestreamRequest.getStreamHosts();
// throw exceptions if request contains no stream hosts
if (streamHosts.size() == 0) {
cancelRequest();
}
StreamHost selectedHost = null;
Socket socket = null;
String digest = Socks5Utils.createDigest(this.bytestreamRequest.getSessionID(), this.bytestreamRequest.getFrom(), this.manager.getConnection().getUser());
/*
* determine timeout for each connection attempt; each SOCKS5 proxy has the same amount of time so that the first does not consume
* the whole timeout
*/
int timeout = Math.max(getTotalConnectTimeout() / streamHosts.size(), getMinimumConnectTimeout());
for (StreamHost streamHost : streamHosts) {
String address = streamHost.getAddress() + ":" + streamHost.getPort();
// check to see if this address has been blacklisted
int failures = getConnectionFailures(address);
if ((CONNECTION_FAILURE_THRESHOLD > 0) && (failures < CONNECTION_FAILURE_THRESHOLD)) {
// try establish socket connection
try {
// build SOCKS5 client
final Socks5Client socks5Client = new Socks5Client(streamHost, digest);
// connect to SOCKS5 Proxy with a timeout
socket = socks5Client.getSocket(timeout);
selectedHost = streamHost;
break;
}
catch (TimeoutException e) {
incrementConnectionFailures(address);
}
catch (SmackException e) {
incrementConnectionFailures(address);
}
catch (IOException e) {
incrementConnectionFailures(address);
}
catch (XMPPException e) {
incrementConnectionFailures(address);
}
}
}
// throw exception if connecting to all SOCKS5 proxies failed
if (selectedHost == null || socket == null) {
cancelRequest();
}
// send used-host confirmation
Bytestream response = createUsedHostResponse(selectedHost);
this.manager.getConnection().sendStanza(response);
return new Socks5BytestreamSession(socket, selectedHost.getJID().equals(this.bytestreamRequest.getFrom()));
}
================================ End of solution code ===================================
For testing and verification, I captured the log message to trace the problem with the modified code below:
--------------- Test code ---------------------------
if ((CONNECTION_FAILURE_THRESHOLD > 0) && (failures < CONNECTION_FAILURE_THRESHOLD)) {
// try establish socket connection
try {
// build SOCKS5 client
final Socks5Client socks5Client = new Socks5Client(streamHost, digest);
// connect to SOCKS5 Proxy with a timeout
socket = socks5Client.getSocket(timeout);
selectedHost = streamHost;
logger.info("Found streamHost for transfering file with failture Count: " + Integer.toString(failures) + " at addrees: " + address.toString());
break;
}
catch (TimeoutException e) {
logger.error("TimeoutException while transfering file with failture Count: " + Integer.toString(failures) + " at addrees: " + address.toString() + " Exception: ", e);
incrementConnectionFailures(address);
}
catch (SmackException e) {
logger.error("SmackException while transfering file with failture Count: " + Integer.toString(failures) + " at addrees: " + address.toString() + " Exception: ", e);
incrementConnectionFailures(address);
}
catch (IOException e) {
logger.error("IOException while transfering file with failture Count: " + Integer.toString(failures) + " at addrees: " + address.toString() + " Exception: ", e);
incrementConnectionFailures(address);
}
catch (XMPPException e) {
** logger.error("XMPPException while transfering file with failture Count: " + Integer.toString(failures) + " at addrees: " + address.toString() + " Exception: ", e);**
incrementConnectionFailures(address);
}
}
Below is the trace log of the whole file transfer process
---------------- Trace log -----------------------
09-10 11:21:19.579: D/SMACK(12093): SENT (0):
http://jabber.org/protocol/bytestreamshttp://jabber.org/protocol/ ibb
09-10 11:21:20.029: D/SMACK(12093): RECV (0):
09-10 11:21:20.039: D/SMACK(12093): SENT (0):
09-10 11:21:20.129: E/Jitsi(12093): [10] org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest.accept().246
SmackException while transfering file with failture Count: 0 at addrees: 192.168.1.109:51006 Exception:
09-10 11:21:20.129: E/Jitsi(12093): org.jivesoftware.smack.SmackException: SOCKS5 negotiation failed
09-10 11:21:20.129: E/Jitsi(12093): at org.jivesoftware.smackx.bytestreams.socks5.Socks5Client$1.call(Socks5Client.jav a:105)
09-10 11:21:20.129: E/Jitsi(12093): at org.jivesoftware.smackx.bytestreams.socks5.Socks5Client$1.call(Socks5Client.jav a:80)
09-10 11:21:20.129: E/Jitsi(12093): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-10 11:21:20.129: E/Jitsi(12093): at java.lang.Thread.run(Thread.java:818)
09-10 11:21:22.129: E/Jitsi(12093): [10] org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest.accept().242
TimeoutException while transfering file with failture Count: 0 at addrees: 192.168.56.1:51006 Exception:
09-10 11:21:22.129: E/Jitsi(12093): java.util.concurrent.TimeoutException
09-10 11:21:22.129: E/Jitsi(12093): at java.util.concurrent.FutureTask.get(FutureTask.java:176)
09-10 11:21:22.129: E/Jitsi(12093): at org.jivesoftware.smackx.bytestreams.socks5.Socks5Client.getSocket(Socks5Client. java:115)
09-10 11:21:22.129: E/Jitsi(12093): at org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest.accept(Socks 5BytestreamRequest.java:236)
09-10 11:21:22.129: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.Socks5TransferNegotiator.negotiateIncoming Stream(Socks5TransferNegotiator.java:104)
09-10 11:21:22.129: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.FaultTolerantNegotiator.createIncomingStre am(FaultTolerantNegotiator.java:68)
09-10 11:21:22.129: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer$2.call(IncomingFileTr ansfer.java:186)
09-10 11:21:22.129: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer$2.call(IncomingFileTr ansfer.java:183)
09-10 11:21:22.129: E/Jitsi(12093): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-10 11:21:22.129: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer.negotiateStream(Incom ingFileTransfer.java:190)
09-10 11:21:22.129: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer.access$100(IncomingFi leTransfer.java:57)
09-10 11:21:22.129: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer$1.run(IncomingFileTra nsfer.java:129)
09-10 11:21:22.129: E/Jitsi(12093): at java.lang.Thread.run(Thread.java:818)
09-10 11:21:22.169: E/Jitsi(12093): [10] org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest.accept().250
IOException while transfering file with failture Count: 0 at addrees: 0.0.0.0:51006 Exception:
09-10 11:21:22.169: E/Jitsi(12093): java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 51006): connect failed: ECONNREFUSED (Connection refused)
09-10 11:21:22.169: E/Jitsi(12093): at libcore.io.IoBridge.connect(IoBridge.java:124)
09-10 11:21:22.169: E/Jitsi(12093): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
09-10 11:21:22.169: E/Jitsi(12093): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
09-10 11:21:22.169: E/Jitsi(12093): at java.net.Socket.connect(Socket.java:882)
09-10 11:21:22.169: E/Jitsi(12093): at java.net.Socket.connect(Socket.java:825)
09-10 11:21:22.169: E/Jitsi(12093): at org.jivesoftware.smackx.bytestreams.socks5.Socks5Client$1.call(Socks5Client.jav a:88)
09-10 11:21:22.169: E/Jitsi(12093): at org.jivesoftware.smackx.bytestreams.socks5.Socks5Client$1.call(Socks5Client.jav a:80)
09-10 11:21:22.169: E/Jitsi(12093): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-10 11:21:22.169: E/Jitsi(12093): at java.lang.Thread.run(Thread.java:818)
09-10 11:21:22.169: E/Jitsi(12093): Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
09-10 11:21:22.169: E/Jitsi(12093): at libcore.io.Posix.connect(Native Method)
09-10 11:21:22.169: E/Jitsi(12093): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
09-10 11:21:22.169: E/Jitsi(12093): at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
09-10 11:21:22.169: E/Jitsi(12093): at libcore.io.IoBridge.connect(IoBridge.java:122)
09-10 11:21:22.169: E/Jitsi(12093): … 8 more
09-10 11:21:24.179: E/Jitsi(12093): [10] org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest.accept().242
TimeoutException while transfering file with failture Count: 0 at addrees: 118.200.189.88:51006 Exception:
09-10 11:21:24.179: E/Jitsi(12093): java.util.concurrent.TimeoutException
09-10 11:21:24.179: E/Jitsi(12093): at java.util.concurrent.FutureTask.get(FutureTask.java:176)
09-10 11:21:24.179: E/Jitsi(12093): at org.jivesoftware.smackx.bytestreams.socks5.Socks5Client.getSocket(Socks5Client. java:115)
09-10 11:21:24.179: E/Jitsi(12093): at org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest.accept(Socks 5BytestreamRequest.java:236)
09-10 11:21:24.179: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.Socks5TransferNegotiator.negotiateIncoming Stream(Socks5TransferNegotiator.java:104)
09-10 11:21:24.179: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.FaultTolerantNegotiator.createIncomingStre am(FaultTolerantNegotiator.java:68)
09-10 11:21:24.179: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer$2.call(IncomingFileTr ansfer.java:186)
09-10 11:21:24.179: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer$2.call(IncomingFileTr ansfer.java:183)
09-10 11:21:24.179: E/Jitsi(12093): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-10 11:21:24.179: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer.negotiateStream(Incom ingFileTransfer.java:190)
09-10 11:21:24.179: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer.access$100(IncomingFi leTransfer.java:57)
09-10 11:21:24.179: E/Jitsi(12093): at org.jivesoftware.smackx.filetransfer.IncomingFileTransfer$1.run(IncomingFileTra nsfer.java:129)
09-10 11:21:24.179: E/Jitsi(12093): at java.lang.Thread.run(Thread.java:818)
09-10 11:21:24.219: I/Jitsi(12093): [10] org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest.accept().238
Found streamHost for transfering file with failture Count: 0 at addrees: 118.200.189.88:7777
09-10 11:21:24.219: D/SMACK(12093): SENT (0):