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?
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.