I am writing a server that can connect with various clients to send and receive information. I am using TCP Sockets. Currently, I am faced with a decision. I can either keep every socket connection from the clients open until they disconnect, or I only connect with the client every time there is a prompt from the client.
The former makes it easy for the server to detect a prompt from any socket (if I'm not mistaken, a Socket Selector can achieve that); however, sockets are non-copyable, meaning I can't just store them in a vector. I can store pointers in the vector, but if the actual socket is destroyed, the pointer will be useless. This is why I am inclined to try the latter option; however, I am unsure if this is actually an efficient method of communication.
Basically, I have three questions. Firstly, which of the two methods of socket management should I use, keeping them all open or only connecting when prompted? Secondly, is there a method to store sockets, or socket pointers, in a container, like a vector, reliably? Lastly, is opening and closing a connection every time the server is prompted actually efficient?
Thank you in advance! Please inform me if my question is unclear.