/* Multipath Sender => Server */ #include #include #include #include #include struct Message_header { struct sockaddr_in mps; struct sockaddr_in mpr; int sessionID; }; /* struct Data{ int sessionID; char msg_type; char msg_size; char data[1024]; }; */ main(int argc, char * argv[]) { int sockfd1; int msgsock; int sockfd2; int length; int rval; int crs_portno; int mps_portno; struct sockaddr_in crs; struct sockaddr_in mpsender; struct sockaddr_in mpr; struct hostent * hp; struct Message_header headr; /* struct Data datapacket; */ char frombuf[1024]; char tobuf[1024]; char * crs_name; /* printf("format: mps crs1_hostname crs1_protno csr2_hostname crs2_portno ...\n); */ /* hard code all the connections to CRS's */ sockfd2 = socket(AF_INET, SOCK_STREAM, 0); if(sockfd2 < 0) { perror("Error opening socket\n"); exit(1); } /* Populate crs1,crs2,crs3... from command line arguments */ crs_name = "wetterhorn.uccs.edu"; crs_portno = 1027; hp = gethostbyname(crs_name); if(hp == 0) { fprintf(stderr, "%s: unknown host\n", crs_name); exit(2); } memcpy(&crs.sin_addr, hp->h_addr, hp->h_length); crs.sin_family = AF_INET; crs.sin_port = htons(crs_portno); length = sizeof(mpsender); if(connect(sockfd2, (struct sockaddr *) &crs, length) < 0) { perror("Error connecting to socket\n"); exit(1); } /* Wait for the ready message from CRS */ do{ rval = read(sockfd2, frombuf, sizeof(frombuf)-1); } while(rval < 0); printf("%s\n", frombuf); rval = 0; /* reset values */ /* create a socket to accept connections from various MPRs */ sockfd1 = socket(AF_INET, SOCK_STREAM, 0); if(sockfd1 < 0) { perror("Error opening socket\n"); exit(1); } mps_portno = 1025; mpsender.sin_family = AF_INET; mpsender.sin_addr.s_addr = INADDR_ANY; mpsender.sin_port = htons(mps_portno); if(bind(sockfd1, (const struct sockaddr *) &mpsender, sizeof(mpsender))) { perror("Error binding socket to MPS\n"); exit(1); } length = sizeof(mpsender); if(getsockname(sockfd1, (const struct sockaddr *) &mpsender, &length)) { perror("Error getting socket name"); exit(1); } printf("MPSender(server) is listening to port #: %d\n",ntohs(mpsender.sin_port)); listen(sockfd1,5); while(1) { msgsock = accept(sockfd1, (struct sockaddr *) &mpr, &length); if(msgsock < 0){ perror("Accept error\n"); exit(1); } if(fork() == 0) { do { /* read request from MPR */ rval = read(msgsock, frombuf, sizeof(frombuf)-1); } while(rval < 0); printf("The request from MPR is: %s\n", frombuf); rval = 0; /* here comes the code to construct the reply and chop the reply into various packets */ sprintf(tobuf, "Hello,I am the Server\n"); /* Construct the reply */ /* constructing message header headr.mpr = mpr; headr.mps = mpsender; headr.sessionID = ?; write(sockfd2, &headr, sizeof(headr)); send the Message_header stucture to CRS replicate this piece of code for so many number of CRSs */ /* write to crs */ if(write(sockfd2, tobuf, sizeof(tobuf)-1) < 0) { perror("Error writing to crs"); exit(1); } /* read acknowledgement from MPR */ do{ rval = read(msgsock, frombuf, sizeof(frombuf)-1); } while(rval < 0); printf("%s", frombuf); rval = 0; if(write(msgsock, tobuf, sizeof(tobuf)-1) < 0) { perror("Error writing to mpr"); exit(1); } /* while(/* no more packets) { write(sockfd2, &datapacket, strlen(datapacket)); /* send the data packets to CRS } while(/* no more packets) { write(msgsock, &datapacket, strlen(datapacket)); /* send the data packets to MPR on original connection } */ } /* end of child process */ } /* end while */ } /* end of main */