Since there is no code ... here are some tips:
1 - do you close your connection ??? this will be the main reason that the port is busy.
2 it is safer to work with the socket when your application is stopped, stopped or destroyed .. you can override onpause, onstop or ondesotry .. and free the port before closing the application.
3- make the connection in a separate thread and implement a timer to check whether you really need a port or not (I mean, if your application is live or not) and based on this kill / leave the stream (connection)
4- in the worst case. If you have control over the other side of the connection (server, device ..), try to make your solution more flexible, for example, first determine the range of ports, and then use if available.
luck
Maher abuthraa
source share