powered by Jive Software

Port(7777) has been used sometime weird when use s5b to transfer file

Port 7777 has been used sometime weird, means that I have two phones, one can open port 7777, another one can’t. The reason is that maybe a third app installed on the phone is using the port(7777).

I have seen the snippet which coms from org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy.

if (getLocalSocks5ProxyPort() < 0) {
     int port = Math.abs(getLocalSocks5ProxyPort());
     for (int i = 0; i < 65535 - port; i++) {
          try {
               this.serverSocket = new ServerSocket(port + i);
               break;
          }
          catch (IOException e) {
               // port is used, try next one
           }
     }
}
else {
     this.serverSocket = new ServerSocket(getLocalSocks5ProxyPort());
}

these codes are from public synchronized void start() method. Found that it just to check if port has been using when localSocks5ProxyPort < 0,

when not, doesn’t check weather port is already in use or not.

I changed the snippet to what it is below:

int port = Math.abs(getLocalSocks5ProxyPort());
try {
      this.serverSocket = new ServerSocket(port);
} catch (IOException e) {
     for (int i = 0; i < 65535 - port; i++) {
          try {
               this.serverSocket = new ServerSocket(port + i);
               break;
           }
           catch (IOException e1) {
                // port is used, try next one
           }
     }
} if (this.serverSocket != null) {
     this.serverThread = new Thread(this.serverProcess);
     this.serverThread.start();
} else {
     LOGGER.log(Level.SEVERE, "couldn't setup local SOCKS5 proxy through all ports.");
}

And then everything is ok, the next port will be found if current one is already in use.

Anyone has better idea for this will be appreciated!

From https://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware /smackx/bytestreams/socks5/Socks5Proxy.html#setLocalSocks5ProxyPort(int)

If you set the port to a negative value Smack tries the absolute value and all following until it finds an open port.
So if you set the port to a negative value, then Smack will do what you want without any further code modifications.

oh, I understood what your meaning is. Coding like this is to give developer a choice to choose use default port(7777) or find a unused port.

Did I understand you completely?

Well I’m not sure if I understand you correctly, but I think you get the idea right.

Also just have a look at the source and see what happens when a negative integer is used and when a positive one is used.

Sorry about my poor english.

But I guess you have understood me.

I have read those snippet, the snippet shows that find a unused port when getLocalSocks5ProxyPort() returns negetive, otherwise just to opengetLocalSocks5ProxyPort()'s port.``

So I can set every port I want via setLocalSocks5ProxyPort.

I can also set -1 to let smack find a unused port.