printf("socket s7 is over \n");
memset((char *)&myaddr,0,sizeof(myaddr)); /* clear sockaddr structure */
myaddr.sin_family = PF_INET; /* set family to Internet */
myaddr.sin_addr.s_addr = inet_addr("192.168.0.191");
port = PROTOPORT1; /* use default port number */
myaddr.sin_port = htons((u_short)port);
memset((char *)&myaddr,0,sizeof(myaddr)); /* clear sockaddr structure */
myaddr.sin_family = AF_INET; /* set family to Internet */
myaddr.sin_addr.s_addr = inet_addr("192.168.250.101");
port = PROTOPORT2; /* use default port number */
myaddr.sin_port = htons((u_short)port);
if (connect(s4, (void *)&myaddr, sizeof(myaddr)) < 0) {
printf("connect to middle and to reboot failed \n,%s\n",strerror(errno));
close(s4);
exit(1);
}
printf("connect and report to halt information to middle and connect succeed \n");
if(send(s4,message_str,strlen(message_str),0)<0){
printf("send info to middle to reboot failed \n,%s\n",strerror(errno));
close(s4);
exit(1);
}
printf("send info and wait for middle detatition and make inner to halt \n");
close(s4);
printf("close socket s4 in report() \n");
/*if(recv(s4,buf,sizeof(buf),0)<0){
printf("recv failed \n");
close(s4);
exit(1);
}*/
}
int halt()
{
struct sockaddr_in myaddr;
struct sockaddr_in theiraddr;
int s1,s4; /* socket descriptor */
char * message_str="quit";
int i,alen;
int port; /* protocol port number */
char buf[BUF_SIZE];
int numbytes;
s4 = socket(PF_INET, SOCK_STREAM, 0);
if (s4 < 0) {
printf("socket s4 creation failed \n,%s\n",strerror(errno));
exit(1);
}
printf("create socket s4 over \n");
memset((char *)&myaddr,0,sizeof(myaddr)); /* clear sockaddr structure */
myaddr.sin_family = PF_INET; /* set family to Internet */
myaddr.sin_addr.s_addr = inet_addr("192.168.250.102");
port = PROTOPORT2; /* use default port number */
myaddr.sin_port = htons((u_short)port);
if (bind(s4, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
printf("bind s4 to middle and have get info to reboot failed\n,%s\n ",strerror(errno));
close(s4);
exit(1);
}
printf("bind sockedt s4 to inner right and prepare to reboot \n");
if(listen(s4,BUF_SIZE)<0) {
printf("listen s4 to middle and get info to reboot failed \n,%s\n",strerror(errno));
close(s4);
exit(1);
}
printf("listen the middle to send infomation and keep waiting \n");
alen=sizeof(theiraddr);
if(s1=accept(s4,(struct sockaddr*)&theiraddr,&alen)<0){
printf("MIDDLE: accept s4 from middle and get info to reboot failed\n,%s\n",strerror(errno));
close(s4);
close(s1);
exit(1);
}
printf("accept middle and prepare to reboot \n");
if(recv(s1,buf,sizeof(buf),0)<0){
printf("detation :recv s4 from middle failed \n,%s\n",strerror(errno));
close(s1);
close(s4);
return -1;
}
printf("recv middle information and prepare to judge whether to reboot \n");
if(strcmp(buf,message_str)==0){
return (1);
}
printf("judge over ");
close(s1);
printf("close socket s1 in halt()\n");
close(s4);
printf("close socket s4 in halt()\n");
return 0;
}
if (connect(s3, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
printf("connect s3 to inner failed \n,%s\n",strerror(errno));
close(s3);
return -1;
}
printf("connect to inner in check() is right!\n");
n = send(s3, message_str,strlen(message_str), 0);
if (n <0 ){
printf("send to inner failed \n,%s\n",strerror(errno));
close(s3);
return -1;
}
printf("send info to inner in check() is right!\n");
alen = recv(s3, buf, sizeof(buf), 0);
if (alen < 0){
printf(" recv info from inner failed \n,%s\n",strerror(errno));
close(s3);
return -1;
}
printf("recv info from inner in check() is right!\n");
close(s3);
printf("close s3 in check()!\n");
return 0;
}
void report()
{
int s3;
struct sockaddr_in myaddr;
char * message_str="prepare to stop inner and outer";
int numbytes;
char buf[BUF_SIZE];
int port;
int alen;
if (connect(s3, (void *)&myaddr, sizeof(myaddr)) < 0) {
printf("connect to middle and wait to reboot failed \n,%s\n",strerror(errno));
close(s3);
exit (1);
}
printf("connect in report() to middle and report result is right ! \n");
if(send(s3,message_str,strlen(message_str),0)<0){
printf("send to middle and wait to reboot failed \n,%s\n",strerror(errno));
close(s3);
exit (1);
}
printf("send to middle and give info and prepare to reboot in report()\n");
close(s3);
printf("close s3 in report() \n");
/*if(recv(s3,buf,sizeof(buf),0)<0){
printf("recv failed \n");
close(s3);
exit (1);
}*/
}
int halt()
{
struct sockaddr_in myaddr;
struct sockaddr_in server;
int s3,s2; /* socket descriptor */
char * message_str="quit";
int i,alen;
int port; /* protocol port number */
char buf[BUF_SIZE];
int numbytes;
memset((char *)&myaddr,0,sizeof(myaddr)); /* clear sockaddr structure */
myaddr.sin_family = PF_INET; /* set family to Internet */
myaddr.sin_addr.s_addr = inet_addr("192.168.251.102");
port = PROTOPORT4; /* use default port number */
myaddr.sin_port = htons((u_short)port);
if (bind(s3, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
printf("bind to middle failed\n,%s\n",strerror(errno));
close(s3);
exit (1);
}
printf("bind s3 in halt() to outer is over \n");
if(listen(s3,BUF_SIZE)<0) {
printf("listen from middle and reboot failed \n,%s\n",strerror(errno));
close(s3);
exit (1);
}
printf("listen from middle in halt() is over \n");
alen=sizeof(server);
if(s2=accept(s3,(struct sockaddr*)&server,&alen)<0){
printf("MIDDLE accept from middle to reboot failed\n,%s\n",strerror(errno));
close(s3);
close(s2);
exit(1);
}
printf("accept in halt() info from middle and prepare to reboot \n");
if(recv(s2,buf,sizeof(buf),0)<0){
printf("recv from middle to reboot failed \n,%s\n",strerror(errno));
close(s2);
close(s3);
return -1;
}
printf("recv data in halt() from middle and prepare to reboot \n");
if(strcmp(buf,message_str)==0){
return 1;
}
printf("judge info in halt() from middle and prepare reboot \n");
close(s2);
printf("close socket s2 in halt() \n");
close(s3);
printf("close socket s2 in halt() \n");
struct sockaddr_in myaddr;
struct sockaddr_in server;
int s2,s3;
int i;
char buf[BUF_SIZE];
char *message="quit";
char * message_str="prepare to stop inner and outer";
int alen;
int port,n;
memset((char *)&myaddr,0,sizeof(myaddr)); /* clear sockaddr structure */
myaddr.sin_family = PF_INET; /* set family to Internet */
myaddr.sin_addr.s_addr = inet_addr("192.168.251.101");
port = PROTOPORT4; /* use default port number */
myaddr.sin_port = htons((u_short)port);
printf("recv data in checkouter() from outer is right!\n");
if(strcmp(buf,message_str)==0){
return 1;
}
printf("halting infomation in checkout() to outer have been accepted \n");
close(s3);
printf("close s3 in checkouter() \n");
close(s2);
printf("close s2 in checkouter() \n");
return 0;
}
int check_inner()
{
struct sockaddr_in myaddr;
struct sockaddr_in server;
int s4;
int s1;
int i;
char buf[BUF_SIZE];
char *message="quit";
char * message_str="prepare to stop inner and outer";
int alen;
int port,n;
memset((char *)&myaddr,0,sizeof(myaddr)); /* clear sockaddr structure */
myaddr.sin_family = PF_INET; /* set family to Internet */
myaddr.sin_addr.s_addr = inet_addr("192.168.250.101");
port = PROTOPORT2; /* use default port number */
myaddr.sin_port = htons((u_short)port);
printf("recv in checkinner() from inner is right \n");
if(strcmp(buf,message_str)==0){
return 1;
}
printf("halting in checkinner() infomation to inner have been accepted \n");
close(s4);
printf("close s4 in checkinner() \n");
close(s1);
printf("close s3 in checkinner() \n");
return 0;
}
if (connect(s1, (void *)&myaddr, sizeof(myaddr)) < 0) {
printf("connect s1 failed \n,%s\n",strerror(errno));
close(s1);
exit (1);
}
printf("connect in notice_inner() to inner and info inner to reboot");
if(send(s1,message_str,strlen(message_str),0)<0){
printf("send info to inner to reboot failed\n,%s \n",strerror(errno));
close(s1);
exit (1);
}
printf("send in notice_inner() info to inner and wait to reboot \n");
if (connect(s2, (void *)&myaddr, sizeof(myaddr)) < 0) {
printf("connect outer and to reboot failed\n,%s \n",strerror(errno));
close(s2);
exit (1);
}
printf("connect in notice_outer() to outer and prepare info outer to reboot");
if(send(s2,message_str,strlen(message_str),0)<0){
printf("send to outer to reboot failed\n,%s \n",strerror(errno));
close(s2);
exit (1);
}
printf("send in notice_outer() info to outer and reboot \n");