?? checka.c
字號:
#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include<stdio.h>#include<string.h>main(){ int flag_A=1; int port=8000;while(1) { char *host_name; /* attach parent's shared memory */ ap=(struct area *)shmat(shmid, 0, 0); /* note: shmid is still set correctly */ /* set semaphore */ buf_sem.sem_op=-1; semop(semid, (struct sembuf *) &buf_sem, 1); host_name=ap->Ahost_name_gl; buf_sem.sem_op=1; semop(semid, (struct sembuf *) &buf_sem, 1); struct sockaddr_in pin; struct hostent *server_host_name; char buf[8192]={}; int socket_descriptor;//resolving the host name if((server_host_name=gethostbyname(host_name))==0) { close(socket_descriptor); 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); printf(": Error create socket\n"); continue; }//check connect if(connect(socket_descriptor,(void*)&pin,sizeof(pin))==-1) { close(socket_descriptor); if(flag_A==1) { /* attach parent's shared memory */ ap=(struct area *)shmat(shmid, 0, 0); /* note: shmid is still set correctly */ /* set semaphore */ 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; } else {} printf(": Error connecting socket\n"); 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); /* attach parent's shared memory */ ap=(struct area *)shmat(shmid, 0, 0); /* set semaphore */ 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); printf("Error in send\n"); break; } //check receive if(recv(socket_descriptor, buf, 8192, 0)==-1) { close(socket_descriptor); printf(": Error in receive\n"); break; } flag_A=1; sleep(1); }//check result close(socket_descriptor); sleep(2); }//wai bu change hub while(1)}//main
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -