I mean to say multiple instance of server that is run here and it has to sent all the received messages to all clients connected. How can we detect client disconnections? My understanding is that If the listening socket is not inside an accept() call when the client connects, the incoming connection gets queued.
if you want multiple instances of the server, that is over my head... only way I know to do it is shared memory or interprocess communication of some sort. I don't know if the networking interface / tcpip protocol has something to make that easier.
if you drop a connection, for some amount of time, you just wait for... give it a time slice, and see if it has been reconnected. Your logic needs to ensure that the remote mac address or something matches and if so, put the new connection over the old one and resume. so when you get a new connection, if you have anything in your recently dropped storage/container, see if it matches and handle if it does, else its just a new connection.
I don't recall how to check if a connection has been closed. The web says if you try to read or write you can detect it ... look at some of the online examples.
do you mean to say to write a timeout function that handles for client disconnections by keep on listening for connections and then restart? Any way to find the list of available servers and send that info to client socket, so client can connect to one of them at a time.?
yes, you need to time them out if they do not recover after some reasonable time. For multiple servers, you need to push the restored guy back to the server it was on if it comes back online, which means shared info about dead connections. It would really be a lot easier if you just restart from scratch if dropped, if that is possible.
yes, you can get a list of servers. But what does that mean? are there multiple computers, or just multiple copies of the program, or both (many computers each running many copies?). There are many ways to do that... a shared memory block if all on the same machine, a little UDP broadcast if on a small network, a shared filesystem, whatever you think is appropriate... it can even be human configured text file, if it does not change after setup?
Yes, there are multiple computers that runs several servers. How can UDP broadcast be done to fetch server details as input to all clients?I start the server. I start client1 first and client2 second. When I type something in the console its client1's turn first so he receives the command. When I type something again client2 receives the command and so over and over.
I was wondering if there is a way to send it to all clients at the same time.?
You need to research this, its bigger than anything I have had to deal with.
I mean you "can" use udp -- each server broadcasts and listens, you can make a list from listening. And clients can listen to that, or you can have a master server that hands a client over to a lesser server ... but again, I do not know the best models to use for such a system.
You're not really answering questions about your requirements, just going off and posting different code samples that don't seem directly related to what you want to do.
You started off wanting to know how to write a server. You've posted BSD socket samples, and more sample code using some network library. Now it seems you want some kind of sync between multiple computers ...
This is no way to tackle a complicated problem.
To help yourself, consider:
1. being clear about what you want help with
2. consider suggestions we make, so far you've pretty much ignored everything I've said