I have a Swing client which has a connect and cancel button so it can attempt connection to the server or end a current connection. I’m trying to make it so the client can connect to the server, end the connection and then connect to the server again multiple times.
My understanding is that typically when a client and server end a connection regardless of who ends it the client closes its streams and socket. Obviously then, they cannot be reused for another connection attempt. Right now I have the Socket and stream vars as private instance variables and a method for connecting to server which creates a new socket and then methods for opening and closing streams.
Just wondering how something like this could be typically handled. I’ve thought about having one humongous method which creates new socket, streams and handles all communication and closing of streams and socket, but it seems messy. Or maybe having a new thread create everything and then when the communication is over terminate the thread.
Ideas appreciated.
– Create a separate Thread at the Server end, when the Client connects to the Server.
– Do the process of read and writing onto the
client socketfor that particular client into that particular thread.– And then terminate the Client thread when its done.
– If you again try to connect it, a new thread will span.
– You can always create a
HashMapto keep tab on Client-Socket to Thread relation.