int main(void) { /*Referred code from beej's client-server architecture*/ int sockfd, new_fd; // listen on sock_fd, new connection on new_fd struct addrinfo hints, *servinfo, *p; struct sockaddr_storage their_addr; // connector's address information socklen_t sin_size; struct sigaction sa; int yes=1; char s[INET6_ADDRSTRLEN]; int rv; char buf[MAXDATASIZE]; /* receive buffer */ int numbytes; char *value; struct hostent *Server3_IP = gethostbyname("nunki.usc.edu"); Node *head; head=NULL; //Initially head will be NULL since linklist is empty head = Initialize_Server(head); //Initialize the server memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // use my IP if ((rv = getaddrinfo(NULL,SERVER3_TCP_STATIC_PORT, &hints, &servinfo)) != 0) { fprintf(stderr, "\ngetaddrinfo: %s", gai_strerror(rv)); return 1; } // loop through all the results and bind to the first we can for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("\nserver: socket"); continue; } if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1) { perror("\nsetsockopt"); exit(1); } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("\nserver: bind"); continue; } break; } if (p == NULL) { fprintf(stderr, "\nserver: failed to bind"); return 2; } freeaddrinfo(servinfo); // all done with this structure if (listen(sockfd, BACKLOG) == -1) { perror("\nlisten"); exit(1); } sa.sa_handler = sigchld_handler; // reap all dead processes sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) { perror("\nsigaction"); exit(1); } printf("\nThe server 3 has TCP port number %d and IP address %s\n",SERVER3_TCP_STATIC_PORT_PRINT,inet_ntoa( *((struct in_addr *)Server3_IP->h_addr) )); //booting up while(1) { sin_size = sizeof their_addr; new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if (new_fd == -1) { perror("\naccept"); continue; } inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr),s, sizeof s); if ((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1) //recev { perror("\nrecv"); exit(1); } buf[numbytes]='\0'; //buf holds the key // printf("\n*********NEW REQUEST*********"); printf("\nThe Server 3 has received a request with the key %s from the Server 2 with port number %d and IP address %s",buf,ntohs(get_in_port((struct sockaddr *)&their_addr)),s); if (!fork()) { // this is the child process close(sockfd); // child doesn't need the listener value = Find_Value(buf,head); //server 2 will search in his memory to get value07 /* if(strcmp(value,"invalid") == 0) //will never occur printf("\nServer 3 does not contain that key"); //will never occur*/ if (send(new_fd,value, 7, 0) == -1) //send perror("\nsend"); printf("\nThe Server 3 sends the reply POST %s to Server 2 with port number %d and IP address %s",value,ntohs(get_in_port((struct sockaddr *)&their_addr)),s); close(new_fd); exit(0); } close(new_fd); // parent doesn't need this } return 0; /*Referred code from beej's client-server architecture*/ }
Node * Client_Server1_Communication(Node *head) { /*Code referred from rutgers*/ struct sockaddr_in myaddr; /* our address */ struct sockaddr_in remaddr; /* remote address */ socklen_t addrlen = sizeof(remaddr); /* length of addresses */ int recvlen; /* # bytes received */ int fd; /* our socket */ char buf[BUFSIZE]; /* receive buffer */ char buf1[BUFSIZE]; /* receive buffer */ struct hostent *Server1_IP = gethostbyname("nunki.usc.edu"); char *value; char *value_received_from_server2; /* create a UDP socket */ if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("\ncannot create socket"); exit(1); } /* bind the socket to any valid IP address of nunki.usc.edu and a specific port 21981 */ memset((char *)&myaddr, 0, sizeof(myaddr)); myaddr.sin_family = AF_INET; myaddr.sin_port = htons(SERVER1_UDP_STATIC_PORT); memcpy(&myaddr.sin_addr, Server1_IP->h_addr_list[0], Server1_IP->h_length); if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { perror("\nbind failed"); exit(1); } //booting up printf("\nThe server 1 has UDP port number %d and IP address %s",SERVER1_UDP_STATIC_PORT,inet_ntoa( *((struct in_addr *)Server1_IP->h_addr) )); /* now loop, receiving data and printing what we received */ for (;;) { recvlen = recvfrom(fd, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen); if (recvlen > 0) buf[recvlen] = 0; else { perror("recv"); exit(1); } // printf("\n*******NEW REQUEST*******"); printf("\n"); check_c1_c2++; if(check_c1_c2%2 == 1) printf("\nServer 1 has received a request with key %s from client 1 with port number %d and IP address %s",buf,remaddr.sin_port,inet_ntoa(remaddr.sin_addr)); else if(check_c1_c2%2 ==0) printf("\nServer 1 has received a request with key %s from client 2 with port number %d and IP address %s",buf,remaddr.sin_port,inet_ntoa(remaddr.sin_addr)); value = Find_Value(buf,head); //buf holds key, find corresponding value if(strcmp(value,"invalid") == 0) //Server 1 does not contain that key (buf) { if(check_c1_c2%2==1) { value_received_from_server2=client1_server1_server2_communicate(buf); //make a call to server 2 to get the value that maps to key (buf) } else if(check_c1_c2%2==0) { value_received_from_server2=client2_server1_server2_communicate(buf); //make a call to server 2 to get the value that maps to key (buf) } strcpy(buf1,value_received_from_server2); head = Insert(head, buf, buf1); // add the received value to server 1 data base if (sendto(fd, buf1, strlen(buf1), 0, (struct sockaddr *)&remaddr, addrlen) < 0) //sending value to client 1 perror("\nsendto"); if(check_c1_c2%2 == 1) printf("\nThe Server 1, sent reply POST %s to Client 1 with port number %d and IP addrss %s", buf1,remaddr.sin_port,inet_ntoa(remaddr.sin_addr)); else if(check_c1_c2%2 == 0) printf("\nThe Server 1, sent reply POST %s to Client 2 with port number %d and IP addrss %s", buf1,remaddr.sin_port,inet_ntoa(remaddr.sin_addr)); } else // Server 1 has the corresponding value to key (buf) { sprintf(buf,"%s",value); if (sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&remaddr, addrlen) < 0) //sending value to client 1 perror("sendto"); if(check_c1_c2%2 == 1) printf("\nThe Server 1 sends the reply POST %s to Client 1 with port number %d and IP address %s",buf,remaddr.sin_port,inet_ntoa(remaddr.sin_addr)); //Send it to client 1 else if(check_c1_c2%2 == 0) printf("\nThe Server 1 sends the reply POST %s to Client 2 with port number %d and IP address %s",buf,remaddr.sin_port,inet_ntoa(remaddr.sin_addr)); //Send it to client 1 /*Code referred from rutgers*/ } } return head; }
int _tmain(int argc, _TCHAR* argv[]) { int First_Pass = TRUE; int lastSize = ARRAY_SIZE; int recCnt = 0; int rejCnt = 0; int cycle = 1; headOneType H1; headTwoType H2; recOneType R1; recTwoType R2; char lineBuffer[82]; long double D1, D2, D3, DblDenum, T_last, T_now, val[ARRAY_SIZE]; FILE *inFile1, *inFile2, *outFile; int count, i, j, k, KS, M, group, maxLine, NC, recNum, recSize, status; /*------------------------------------------------------------------------*/ /* Open data files; quit if unable to open any of them. */ /*------------------------------------------------------------------------*/ if ( argc == 4 ) { inFile1 = fopen(argv[1],"r"); inFile2 = fopen(argv[2],"r"); outFile = fopen(argv[3],"wb"); } else { printf("\n Improper number of command line arguments.\n\n"); printf("\n usage:"); printf("\n convert.exe <header.file> <ascii epemeris.file> <binary output> "); exit(1); } if ( (inFile1==NULL) | (inFile2==NULL) | (outFile==NULL) ) { if (inFile1==NULL) printf("\n Unable to open header file.\n\n"); if (inFile2==NULL) printf("\n Unable to open ascii ephemeris file.\n\n"); if (outFile==NULL) printf("\n Unable to open binary output ephemeris file.\n\n"); exit(1); } /*------------------------------------------------------------------------*/ /* Initialize constant name & value arrays. */ /*------------------------------------------------------------------------*/ for ( i=0 ; i<400 ; i++ ) { for (j=0 ; j<6 ; j++ ) R1.constName[i][j] = ' '; R2.constValue[i] = 0.0; } /*------------------------------------------------------------------------*/ /* Read first line from header file (into dummy variables). */ /*------------------------------------------------------------------------*/ status = Read_File_Line(inFile1,FALSE,lineBuffer); sscanf(lineBuffer,"KSIZE= %d NCOEFF= %d",&KS,&NC); /*------------------------------------------------------------------------*/ /* Read data from Group 1010 */ /*------------------------------------------------------------------------*/ group = Read_Group_Header(inFile1); if ( group == 1 ) { for ( i=0 ; i<3 ; i++ ) { status = Read_File_Line(inFile1,FALSE,lineBuffer); for (j=0 ; j<81 ; j++) R1.label[i][j] = lineBuffer[j]; } } else { Warning(7); exit(1); } /*............................Convert <cr> to end of string marker ('\0') */ for (i=0 ; i<3 ; i++) for (j=0 ; j<81 ; j++) if ( R1.label[i][j] == '\r') R1.label[i][j] = '\0'; /*------------------------------------------------------------------------*/ /* Read data from Group 1030 */ /*------------------------------------------------------------------------*/ group = Read_Group_Header(inFile1); if ( group == 2 ) { status = Read_File_Line(inFile1,FALSE,lineBuffer); sscanf(lineBuffer," %10Le",&R1.timeData[0]); sscanf(lineBuffer+14," %10Le",&R1.timeData[1]); sscanf(lineBuffer+27," %10Le",&R1.timeData[2]); } else { Warning(8); exit(1); } /*------------------------------------------------------------------------*/ /* Read data from Group 1040 */ /*------------------------------------------------------------------------*/ group = Read_Group_Header(inFile1); if ( group == 3 ) { status = Read_File_Line(inFile1,FALSE,lineBuffer); sscanf(lineBuffer," %d",&M); count = 0; while ( count < M ) { status = Read_File_Line(inFile1,FALSE,lineBuffer); for ( i=0 ; i<10 ; i++ ) { sscanf(lineBuffer+(i*8)," %6c",&R1.constName[count]); if (++count == M) break; } } } else { Warning(9); exit(1); } /*------------------------------------------------------------------------*/ /* Read data from Group 1041 */ /*------------------------------------------------------------------------*/ group = Read_Group_Header(inFile1); if ( group == 4 ) { status = Read_File_Line(inFile1,FALSE,lineBuffer); sscanf(lineBuffer," %d",&NC); /* NC now means "Number */ if ( M == NC ) /* of Constants"; it is */ { /* no longer a "dummy". */ count = 0; while ( count < NC ) { status = Read_File_Line(inFile1,TRUE,lineBuffer); sscanf(lineBuffer," %Le %Le %Le",&D1,&D2,&D3); R2.constValue[count] = D1; if (++count == NC) break; R2.constValue[count] = D2; if (++count == NC) break; R2.constValue[count] = D3; if (++count == NC) break; } } else { Warning(10); exit(1); } } else { Warning(11); exit(1); } /*------------------------------------------------------------------------*/ /* Read data from Group 1050 */ /*------------------------------------------------------------------------*/ group = Read_Group_Header(inFile1); if ( group == 5 ) { for ( i=0 ; i<3 ; i++ ) { status = Read_File_Line(inFile1,FALSE,lineBuffer); for ( j=0 ; j<13 ; j++ ) { if (j<12) sscanf(lineBuffer+(j*6),"%3ld",&R1.coeffPtr[j][i]); else sscanf(lineBuffer+(j*6),"%3ld",&R1.libratPtr[i]); } } } else { Warning(12); exit(1); } /*------------------------------------------------------------------------*/ /* The binary header record contains some constants whose values are */ /* assigned in the (ASCII) header file. These values are extracted and */ /* assigned to local variables here: */ /*------------------------------------------------------------------------*/ R1.numConst = (long int) NC; R1.AU = Find_Value("AU ",R1.constName,R2.constValue); R1.EMRAT = Find_Value("EMRAT ",R1.constName,R2.constValue); DblDenum = Find_Value("DENUM ",R1.constName,R2.constValue); R1.DENUM = (long int) DblDenum; H1.data = R1; H2.data = R2; /*------------------------------------------------------------------------*/ /* Write binary header records. */ /*------------------------------------------------------------------------*/ fwrite(&H1,sizeof(H1),1,outFile); fwrite(&H2,sizeof(H2),1,outFile); /*------------------------------------------------------------------------*/ /* Main Loop: Read ASCII data and write binary data. */ /*------------------------------------------------------------------------*/ while (!feof(inFile2)) { /*.........................................Increment record counter */ recCnt = recCnt + 1; /*..........................Read first line of current ASCII record */ status = Read_File_Line(inFile2,FALSE,lineBuffer); if (status == EOF) /* Nominal program exit */ break; else sscanf(lineBuffer," %d %d ",&recNum,&recSize); /*................................Check for record size consistancy */ if ( recSize != lastSize ) { Warning(13); exit(1); } lastSize = recSize; /*................Compute the number of lines with coefficient data */ if (mod(recSize,3)==0) maxLine = recSize/3; else maxLine = recSize/3 + 1; /*..................................Read coefficients into an array */ for ( i=0 ; i<maxLine ; i++ ) { status = Read_File_Line(inFile2,TRUE,lineBuffer); if ( status == EOF ) { Warning(22); break; } sscanf(lineBuffer,"%Le",&val[3*i]); if (3*i+1 >= recSize) break; sscanf(lineBuffer+26,"%Le",&val[3*i+1]); if (3*i+2 >= recSize) break; sscanf(lineBuffer+52,"%Le",&val[3*i+2]); } /*........................................Read start time of record */ T_now = val[0]; /*........................Write the coefficients to the binary file */ if ( First_Pass ) /* Will force write of first record */ { T_last = val[0]; First_Pass = FALSE; } if ( T_now == T_last ) /* Write only if no gap between records */ { cycle = cycle + 1; if ( cycle == 26 ) /* Let the user know all is well */ { printf("\n Writing record: %d",recCnt); cycle = 1; } for ( i=0 ; i<recSize ; i++ ) /* Write binary data record */ { fwrite(&val[i],sizeof(long double),1,outFile); } T_last = val[1]; /* Save stop time of record */ } else { if (rejCnt == 0) printf("\n"); rejCnt = rejCnt + 1; printf(" Record %d rejected:",recCnt); printf(" T_last = % 4d , ",T_last); printf("T_now = % 4d\n",val[0]); if (rejCnt > 10) return 1; /* Major problem, quit */ } } /*.................................................End: Main Loop */ /*------------------------------------------------------------------------*/ /* Close files, print summary, and quit. */ /*------------------------------------------------------------------------*/ fclose(inFile1); fclose(inFile2); fclose(outFile); printf("\n\n Data Conversion Completed. \n"); printf("\n Records Converted: % 3d",recCnt); printf("\n Records Rejected: % 2d\n\n",rejCnt); return 0; } /**======================================================== End: convert.c **/