/* Multipath Receiver => Client */ #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 rval1; int rval2; int mps_portno; int mpr_portno; struct sockaddr_in crs; struct sockaddr_in mps; struct sockaddr_in mpreceiver; /* struct Message_header headr; */ struct hostent * hp; char frombuf1[1024]; char frombuf2[1024]; char tobuf[1024]; char * mps_name; /* printf("format: mpr servername serverport#\n"); */ sockfd1 = socket(AF_INET, SOCK_STREAM, 0); if(sockfd1 < 0) { perror("Error opening socket"); exit(1); } /* here comes the code to populate mps */ mps_name = "blanca.uccs.edu"; /* argv[1]; */ /* mps_portno = atoi(argv[2]); if(mps_portno < 1024) */ mps_portno = 1025; printf("The server's host name is %s, port_no = %d\n", mps_name, mps_portno); hp = gethostbyname(mps_name); if(hp == 0) { fprintf(stderr, "%s: unknown host\n", mps_name); exit(2); } memcpy(&mps.sin_addr, hp->h_addr, hp->h_length); mps.sin_family = AF_INET; mps.sin_port = htons(mps_portno); length = sizeof(mps); if(connect(sockfd1, (struct sockaddr *) &mps, length) < 0) { perror("Error connecting to socket"); exit(1); } /* here comes the code for constructing the request */ sprintf(tobuf, "Hello,I am a Client\n"); /* Construct the request */ if(write(sockfd1, tobuf, sizeof(tobuf)-1) < 0) { /* send request to MPS */ perror("Error writing on socket"); exit(1); } /* create a socket to listen from various CRSs */ sockfd2 = socket(AF_INET, SOCK_STREAM, 0); if(sockfd1 < 0) { perror("Error opening socket\n"); exit(1); } mpr_portno = 1050; mpreceiver.sin_family = AF_INET; mpreceiver.sin_addr.s_addr = INADDR_ANY; mpreceiver.sin_port = htons(mpr_portno); if(bind(sockfd2, (const struct sockaddr *) &mpreceiver, sizeof(mpreceiver))) { perror("Error binding socket to MPR\n"); exit(1); } length = sizeof(mpreceiver); if(getsockname(sockfd2, (const struct sockaddr *) &mpreceiver, &length)) { perror("Error getting socket name"); exit(1); } printf("MPReceiver is listening to port #: %d\n",ntohs(mpreceiver.sin_port)); /* send the portno to MPS */ /* write(sockfd1, ntohs(mpreceiver.sin_port),? */ listen(sockfd2,5); while(1) { msgsock = accept(sockfd2, (struct sockaddr *) &crs, &length); if(msgsock < 0){ perror("Accept error"); exit(1); } /* send Ready message to CRS and MPR */ sprintf(tobuf, "ACK: MPR is ready to read reply"); write(msgsock, tobuf, sizeof(tobuf)-1); /* Read reply from CRS */ do { rval1 = read(msgsock, frombuf1, sizeof(frombuf1)-1); } while(rval1 < 0); printf("Reply from crs is: %s \n", frombuf1); write(sockfd1, tobuf, sizeof(tobuf)-1); /* Read reply from MPS */ do{ rval2 = read(sockfd1, frombuf2, sizeof(frombuf2)-1); }while(rval2 < 0); printf("Reply from mps is: %s \n", frombuf2); } /* end while */ /* here comes the code for re-ordering all the packets and sending to the client */ } /* end main */