The simulation of hot standby (client-side adaptive mode) under Unix/linux has aroused a great interest in the process of switching between two machines. In the evening to write a code simulation, the process of switching between two machines. The simulation method is realized by the client adaptive method. When a client discovers that a linked server is closed, the standby server is linked. The description is as follows: The two server processes are listening on both ports of 3000,3001, and when there is a client process linking the service process, the service process creates a thread to communicate with it. Initiates a client process that initializes the 3000-port service process linked by the client process, and when the 3000-port service process is found to be updated and stopped, the client attempts to link the service process to another port. How to repeat, so that you can implement and synchronize the update service process without stopping normal service to the client.
The code is as follows
Client code:
#include <iostream> #include <stdlib.h> #include "client.h" using namespace std;
Char strmsg[30] = "Hello server.";
int serverport[2] = {3000, 3001}; void Initclient (client & client, int index) {if (!client.initialize ()) {cout << "client initialize error P
Rogram exit "<< Endl;
Exit (1); } if (!client.connection (char *) server_ip, Serverport[index)) {cout << Client connection Error program Exi
T "<< Endl;
cout << "server_ip =" << server_ip << Endl;
cout << "Server_port =" << Serverport[index] << Endl;
Exit (1);
{int main (int argc, char * argv[]) {int index = 0;
Client client;
Initclient (client, index);
int count = 0;
BOOL flag = TRUE;
Sleep (2);
bool out = true;
while (1) {if (flag) {flag = client.recvmsg (STRMSG);
} if (flag) {if (out) {cout << "connection server" << Endl;
out = false;
Sleep (2); else {cout<< "one server port close" << Endl;
cout << "Change the Another server port" << Endl;
index++;
Index%= 2;
Initclient (client, index);
Flag = true;
out = true;
Sleep (2);
} return 0; }
Service-Side code:
#include <iostream> #include <stdlib.h> #include <pthread.h> #include <list> #include
Adapter.h "#include" server.h "using namespace std;
int Nnewsocketid;
void * Run (void * arg) {Clientadapter * Padapter = (Clientadapter *) arg;
Padapter->createclientadapter ();
int main (int argc, char * argv[]) {if (argc!= 2) {cout << "arg error!" << Endl;
int listenport = atoi (argv[1]);
List<clientadapter * > Vadapter;
pthread_t Tid;
struct SOCKADDR client;
Server server;
Server.initialize ();
Server.bindport (Listenport);
Server.listenclient ();
int clientcnt = Vadapter.size ();
cout << "Client count =" << clientcnt << Endl; while (true) {if (clientcnt!= vadapter.size ()) {cout << "client count =" << vadapter.size () <
< Endl;
clientcnt = Vadapter.size ();
} Nnewsocketid = server.acceptclient (client);
if (Nnewsocketid = = 1) {//perror ("accept");
Sleep (2); } ElSE {clientadapter * pclient = new Clientadapter ();
PCLIENT->M_NSOCKETFD = Nnewsocketid;
cout << "Get the Client connection." << Endl;
cout << "New Socketid =" << nnewsocketid << Endl;
Vadapter.push_back (pclient);
Pthread_create (&tid, NULL, Run, (void *) pclient);
} list<clientadapter *>::iterator ITR = Vadapter.begin ();
while (ITR!= vadapter.end ()) {if (*ITR)->m_bifcanerase) {Delete (*ITR);
Vadapter.erase (itr++);
Continue
else {itr++;
}} return 0;
}
Client.cpp:
#include "client.h"
client::client ()
{
//nothing.
}
Client::~client ()
{
//nothing.
}
BOOL Client::initialize ()
{
M_NSOCKETFD = socket (af_inet, sock_stream, 0);
if ( -1 = m_nsocketfd)
{
perror ("socket create");
return false;
}
else
{return
true;
}
}
BOOL Client::connection (char * pstrserverip, int serverport)
{
m_sserver.sin_family = af_inet;
M_SSERVER.SIN_ADDR.S_ADDR = inet_addr (Pstrserverip);
M_sserver.sin_port = htons (serverport);
If Connect (m_nsocketfd, (struct sockaddr *) &m_sserver, sizeof (m_sserver)) < 0)
{
perror ("Connecting Stream socket ");
return false;
}
else
{
int nflag = FCNTL (m_nsocketfd,f_getfl,0);
Fcntl (m_nsocketfd,f_setfl,nflag| O_nonblock);
return true;
}
BOOL Client::recvmsg (char * pstrmsg)
{
char buf[100];
Buf[0] = 0;
if (recv (M_NSOCKETFD, buf, 0) <= 0) {return
false;
}
return true;
}
Server.cpp:
#include "Server.h" Server::server () {//nothing.}
Server::~server () {//nothing.}
BOOL Server::initialize () {M_NSOCKETFD = socket (af_inet, sock_stream, 0);
if (M_nsocketfd < 0) {perror ("opening stream socket");
return false;
int nflag = FCNTL (m_nsocketfd,f_getfl,0); Fcntl (m_nsocketfd,f_setfl,nflag|
O_nonblock);
int on = 1;
SetSockOpt (M_NSOCKETFD, Sol_socket, so_reuseaddr, &on, sizeof (on));
return true;
BOOL Server::bindport (int nserverport) {m_sserver.sin_family = af_inet;
M_sserver.sin_port = htons (Nserverport);
M_SSERVER.SIN_ADDR.S_ADDR = Inaddr_any;
Bzero (& (M_sserver.sin_zero), 8);
if (Bind (M_NSOCKETFD, (struct sockaddr *) &m_sserver, sizeof (m_sserver)) < 0) {perror ("binding stream socket");
return false;
else {return true;
} void Server::listenclient () {Listen (M_NSOCKETFD, 5);}
int server::acceptclient (SOCKADDR & clientaddr) {int len = sizeof (struct sockaddr); return Accept (M_NSOCKETFD, (struct sockaddr *) &clientaddr, (socklen_t *) &len);
}
Adpater.cpp:
#include "Adapter.h"
clientadapter::clientadapter ()
{
//nothing.
This->m_bifcanerase = false;
}
Clientadapter::~clientadapter ()
{
//nothing.
}
void Clientadapter::createclientadapter ()
{
int rval;
Char buf[1024];
pthread_t Tid;
Tid = Pthread_self ();
printf ("Thread id =%u\n", (unsigned int) tid);
cout << "Socket Id =" << m_nsocketfd << Endl;
while (true)
{
int sendlen = Send (M_NSOCKETFD, "Hello client", 0);
Sleep (2);
if (sendlen <= 0)
{
printf ("Ending connection\n");
M_bifcanerase = true;
break;
}}}
The results of the operation are as follows:
Simulation Results Analysis: Its two server-side processes, and then a client process is restarted. Stops at the 3000-port listening server process, at which point the client process discovers that the service process that is listening on the 3000 port has stopped and links the service-side process that is listening on port 3001. Then restart the 3000-port listening service process, stopping at the 3001-Port listening service process. At this point, the client process found that the service process that was listening on port 3001 was stopped and then went to link the service-side process listening on port 3000