check_astate.h
来自「linux下的人机对话编程」· C头文件 代码 · 共 136 行
H
136 行
#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include<stdio.h>#include<string.h>void check_A(){ int flag_A=1; int port=8040; struct sockaddr_in pin; struct hostent *server_host_name; char buf[8192]={}; int socket_descriptor; char *host_name; /* attach parent's shared memory */ ap=(struct area *)shmat(shmid, 0, 0); /* set semaphore */while(1) { buf_sem.sem_op=-1; semop(semid, (struct sembuf *) &buf_sem, 1); host_name=ap->Ahost_name_gl; printf("A:%s\n",host_name); buf_sem.sem_op=1; semop(semid, (struct sembuf *) &buf_sem, 1);//resolving the host name if((server_host_name=gethostbyname(host_name))==0) { close(socket_descriptor); if(flag_A==1) { buf_sem.sem_op=-1; semop(semid, (struct sembuf *) &buf_sem, 1); ap->Astate_gl=0; buf_sem.sem_op=1; semop(semid, (struct sembuf *) &buf_sem, 1); flag_A=0; } sleep(1); printf(": Error resolving local host\n"); continue; } //init the port now// bzero(&pin,sizeof(pin));// pin.sin_family=AF_INET;// pin.sin_addr.s_addr=htonl(INADDR_ANY);// pin.sin_addr.s_addr=((struct in_addr*)(server_host_name->h_addr))->s_addr; // pin.sin_port=htons(port);//create socket if((socket_descriptor=socket(AF_INET,SOCK_STREAM,0))==-1) { close(socket_descriptor); if(flag_A==1) { buf_sem.sem_op=-1; semop(semid, (struct sembuf *) &buf_sem, 1); ap->Astate_gl=0; buf_sem.sem_op=1; semop(semid, (struct sembuf *) &buf_sem, 1); flag_A=0; } printf(": Error create socket\n"); sleep(1); continue; }bzero(&pin,sizeof(pin));memcpy(&pin.sin_addr,server_host_name->h_addr,server_host_name->h_length);pin.sin_family=server_host_name->h_addrtype;pin.sin_port=htons(port);//check connect// if(connect(socket_descriptor,(void*)&pin,sizeof(pin))==-1) if(connect(socket_descriptor,(struct sockaddr *)&pin,sizeof(pin))==-1) { close(socket_descriptor); if(flag_A==1) { //buf_sem.sem_op=-1; //semop(semid, (struct sembuf *) &buf_sem, 1); ap->Astate_gl=0; // buf_sem.sem_op=1; //semop(semid, (struct sembuf *) &buf_sem, 1); flag_A=0; } printf(": Error connecting socket\n"); sleep(1); continue; } while(1) { char buf[8192]={}; char *str="check A ok?"; //check send if(send(socket_descriptor,str,strlen(str),0)==-1) { close(socket_descriptor); buf_sem.sem_op=-1; semop(semid, (struct sembuf *) &buf_sem, 1); ap->Astate_gl=0; buf_sem.sem_op=1; semop(semid, (struct sembuf *) &buf_sem, 1); flag_A=0; printf("Error in send\n"); break; } //check receive if(recv(socket_descriptor, buf, 8192, 0)==-1) { close(socket_descriptor); buf_sem.sem_op=-1; semop(semid, (struct sembuf *) &buf_sem, 1); ap->Astate_gl=0; buf_sem.sem_op=1; semop(semid, (struct sembuf *) &buf_sem, 1); flag_A=0; printf(": Error in receive\n"); break; } if(flag_A==0) { buf_sem.sem_op=-1; semop(semid, (struct sembuf *) &buf_sem, 1); ap->Astate_gl=1; buf_sem.sem_op=1; semop(semid, (struct sembuf *) &buf_sem, 1); flag_A=1; } sleep(1); }//while()//check result close(socket_descriptor); sleep(5); }//wai bu change hub while(1)}//main
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?