You cannot associate two tcp server sockets with the same port. reuseAddress valid for client sockets, and it doesn't work the way you think it does ... and the way you use it will not do anything anyway (because you install it after binding).
You really don't need to bind to the same port twice. Just delete this line tcpServer = new TcpServer(LocalPort); from the bottom of your while , and everything will be installed.
How it works, you bind your server socket once and listen on the port. When the connection arrives, it expands the client socket so that you can communicate with the client, and the original server socket continues to listen for more connections.
Basically, you need to remove the socket member (and any other state) from your TcpServer and force the accept method to return the received socket. Then make your runnable socket as a parameter instead of TcpServer and use it to serve the client connection. Then just keep calling accept in a loop and format the streams for new connections just like you know, except that don't recreate the server every time.
Or, conversely, remove the server socket and port from TcpServer , create a socket outside the loop, then while(true) call accept on it, create a new TcpServer with the client socket returned and use it in the stream to process the connection.
Remember to close client sockets after you are done with them.
source share