My server program (socket stream) is running and it accepts the clients. Due to some abnormal condition, server is getting terminated. The other side clients are waiting for server reply. How to I reconnect the running clients to new server? any functions in sockets?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
You cant handle this in server but you can create a session for your clients then when your client reconnect restore their settings and continue on for sending and receiving messages and in your client side application create a thread with specific interval to check if the server is available or not and if so try a reconnect procedure but, I suggest you to check your server side program what happens that your program goes down?
refered by Advanced programming in the unix Environment book.
You can also use:
SO_REUSEADDR
insetsockopt()
. It allows the reuse of local addresses.Let me start by saying that anything is possible. There is a function which does that for you. It is the same
connect
that you might have used for your TCP client. You just need to think about when you need to call this connect again.So when do I use that
connect
function now?Let me present one possible solution.
You need to have some sort of monitoring software (maybe a daemon) which keeps track of the state of the server process. It could, say, periodically poke the server process to see if it's alive.
Consider the case of a single client and server. The client is running on system A; the server, on system B.
Say the server has run and crashed right before it
recv
ed anything. This means the client would have successfully connected to the server and itssend
will fail. When thesend
fails, you can contact your monitoring software on system B to see what happened.If the monitoring software reports that it didn't find anything wrong with the server, something else went wrong then (possibly an interrupt, your NIC went kaput, whatever). Those reasons are outside the scope of this discussion.
If your monitoring software replies saying that it found that the server program died, then you can:
Now, in the client in system A, begin the process of
socket
,connect
,send
,recv
, etc again.Essentially, you are creating another server, X, which looks after your current server, Y. When server Y dies, you look to server X for reasons.
You connect to the new server the same way you connected to the original server. There isn't a different API for this. I don't see why you would think otherwise..
A socket that had been
connect()
ed once cannot be reused with another call toconnect()
.The steps to connect to a TCP server and read/write some data are as follows (pseudo code):
In case the server goes down after
connect
all the client could and shall do isand then start over beginning with:
Starting over and beginning with:
would probably lead to undefined behaviour, but at least to an error.