in my app I am using react-native-tcp-socket (https://github.com/Rapsssito/react-native-tcp-socket?tab=readme-ov-file) to start a server on a hard-coded port.
The application starts fine initially, but my problem is that I am unable to release the resources when my app restarts (e.g. on app reload after code change) and I get an error that states that the address is already in use (Exception on socket 0 java.net.BindException: bind failed: EADDRINUSE (Address already in use))
Here is part of my code:
class TCPServer {
static ipAddress = '0.0.0.0';
static port = <my-port>;
static server = null;
constructor() {
const serverInstance = TcpSocket.createServer((socket) => {
...
...
...
}).listen({ port: TCPServer.port, host: TCPServer.host });
}
static getInstance() {
if (TCPServer.server === null) {
TCPServer.server = new TCPServer();
}
return TCPServer.server;
}
static release() {
if (TCPServer.server) {
TCPServer.server.close();
}
}}
App.js:
tcpServer = undefined;
useEffect(() => {
console.log("OPENING THE SOCKET!");
tcpServer = TCPServer.getInstance();
return () => {
console.log("CLOSING THE SOCKET!");
TCPServer.release();
};
}, [])
It appears that the release function is not called as I am not seeing the message "CLOSING THE SOCKET!" in the logs.