Android Question Ports issue

yaniv hanya

Active Member
Licensed User
When I was playing with the FileTransfer tutorial , I got the following exception:
"java.net.BindException: Address already in use"
(It happened because I ran two services that listened to the same port)
And I wondered, because the port we use is written in code, can't another application already use it?
And if so, is there anything to do programmatically.
And how can one check if the port is free before trying to connect to it?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
When I was playing with the FileTransfer tutorial
Why?

This is an old tutorial which points you to a newer, simpler and more powerful solution.

And I wondered, because the port we use is written in code, can't another application already use it?
It can happen. Not very likely if you choose a random port as there aren't many things running in Android.

You can catch this error and switch to a different port. The problem is that the other app that connects to this one will also need to switch to a different port.

Another option is to use auto discovery: https://www.b4x.com/android/forum/threads/mqtt-chat-with-auto-discovery.75713/#content
However it can also fail in some cases based on the network configuration.
 
Upvote 0
Top