Пример #1
0
int main(int argc, char *argv[]){

	int sockfd;
	FILE * testfile;
	int filelength, len;
	char * fname, * s;
	char msg[MSG_LEN];
	int sent = 0;
	struct timeval starttime, endtime;
	double lapsed;
	
	if (argc != 3) {
		printf("Usage: %s 'server hostname' 'filename'\n", argv[0]);
		exit(0);
	}	

	/* update random seed */
	srand(time(NULL));
	/* remove the above line if you want to get the same random 
	   sequence for each run - good for testing */
	
	/* read in packet loss rate and error rate */
	s = getenv("PACKET_LOSS_RATE");
	if (s != NULL) LOSS_RATE = strtof(s, NULL);
	s = getenv("PACKET_ERR_RATE");
	if (s != NULL) ERR_RATE = strtof(s, NULL);
	printf("PACKET_LOSS_RATE = %.2f, PACKET_ERR_RATE = %.2f\n", LOSS_RATE, ERR_RATE);

	fname=argv[2];
	//open file
	if (!(testfile = fopen(fname, "r"))) {
		printf("Open file failed.\nProgram terminated.");
		exit(0);
	}
	printf("Open file successfully \n");
	//get the file size
	fseek(testfile, 0L, SEEK_END); 
	filelength = ftell(testfile); 	
	printf("File bytes are %d \n",filelength);
	fseek(testfile, 0L, SEEK_SET);
		
	// create RDT socket
    sockfd = rdt_socket();
    
    //specify my own IP address & port number, because if I do not specify, others can not send things to me.
	rdt_bind(sockfd, CPORT);     
    
	//specify the IP address & port number of my partner 
    rdt_target(sockfd, argv[1], SPORT);

    /* a very simple handshaking protocol */ 
    //send the size of the file
    memset(msg, '\0', MSG_LEN);
    sprintf(msg, "%d", filelength);
    rdt_send(sockfd, msg, strlen(msg));  
      
    //send the file name to server
    rdt_send(sockfd, fname, strlen(fname));
    
    
	//wait for server response
    memset(msg, '\0', MSG_LEN);
	len = rdt_recv(sockfd, msg, MSG_LEN);
	if (strcmp(msg, "ERROR") == 0) {
		printf("Server experienced fatal error.\nProgram terminated.\n");
		goto END;
	} else
		printf("Receive server response\n");

	/* start the data transfer */
	printf("Start the file transfer . . .\n");
	gettimeofday(&starttime, NULL);
    // send the file contents
    while (sent < filelength) {
    	if ((filelength-sent) < MSG_LEN)
    		len = fread(msg, sizeof(char), filelength-sent, testfile);
    	else
    		len = fread(msg, sizeof(char), MSG_LEN, testfile);
    	rdt_send(sockfd, msg, len);
    	sent += len;
		usleep(1000);
    }
	gettimeofday(&endtime, NULL);
	printf("Complete the file transfer.\n");
	lapsed = (endtime.tv_sec - starttime.tv_sec)*1.0 + (endtime.tv_usec - starttime.tv_usec)/1000000.0;
	printf("Total elapse time: %.3f s\tThroughtput: %.2f KB/s\n", lapsed, filelength/lapsed/1000.0);
	
END:    
    // close the file
    fclose(testfile);
    
    // close the rdt socket
    rdt_close(sockfd);
	printf("Client program terminated\n");

    return 0;
}
Пример #2
0
int main(int argc, char *argv[]) {

	int sockfd;
	char filepath[200]; 
	char * s;
	char msg[MSG_LEN];
	struct stat sbuf;	
	FILE * testfile;
	int file_len, len;
	int received=0;
	
	if (argc != 2) {
		printf("Usage: %s 'client hostname'\n", argv[0]);
		exit(0);
	}
	
	/* update random seed */
	srand(time(NULL));
	/* remove the above line if you want to get the same random 
	   sequence for each run - good for testing */
	
	/* check whether the folder exists */
	if (stat(STORAGE, &sbuf) != 0) {
		printf("Directory ./%s does not exist!!\n", STORAGE);
		printf("Please create the directory %s before start up the server.\n", STORAGE);
		exit(0);
	}
		
	/* read in packet loss rate and error rate */
	s = getenv("PACKET_LOSS_RATE");
	if (s != NULL) LOSS_RATE = strtof(s, NULL);
	s = getenv("PACKET_ERR_RATE");
	if (s != NULL) ERR_RATE = strtof(s, NULL);
	printf("PACKET_LOSS_RATE = %.2f, PACKET_ERR_RATE = %.2f\n", LOSS_RATE, ERR_RATE);
	
	// create RDT socket
    sockfd = rdt_socket();
    
    //specify my own IP address & port number, because if I do not specify, others can not send things to me.
	rdt_bind(sockfd, SPORT);    

	//specify the IP address & port number of my partner 
    rdt_target(sockfd, argv[1], CPORT);
    
	/* a very simple handshaking protocol */
	// wait for client request
	memset(msg, '\0', MSG_LEN);
	len = rdt_recv(sockfd, msg, MSG_LEN);
	file_len = atoi(msg);
	printf("Received client request: file size = %d\n", file_len);
	
	memset(msg, '\0', MSG_LEN);
	len = rdt_recv(sockfd, msg, MSG_LEN);
	sprintf(filepath, "%s/%s", STORAGE, msg);
	testfile = fopen(filepath, "w");
	if (!testfile) {
		printf("Cannot open the target file: ./%s for write\n", filepath);
		
		// send the ERROR response
		memset(msg, '\0', MSG_LEN);
		sprintf(msg, "ERROR");
		rdt_send(sockfd, msg, strlen(msg));
		goto END;
	} else {
		printf("Open file %s for writing successfully\n", filepath);
		// send the ERROR response
		memset(msg, '\0', MSG_LEN);
		sprintf(msg, "OKAY");
		rdt_send(sockfd, msg, strlen(msg));
	}

	/* start the file transfer */   
	printf("Start receiving the file . . .\n");
    // receive the file contents
    while (received < file_len) {
    	memset(msg, 0, MSG_LEN);
    	len = rdt_recv(sockfd, msg, MSG_LEN);
    	fwrite(msg, sizeof(char), len, testfile);
    	received += len;
    	printf("Received a message of size %d bytes\n", len);
    }

	printf("Complete the file transfer.\n");
	
END:    
    // close the file
    fclose(testfile);
    
    // close the rdt socket
    rdt_close(sockfd);
    
    printf("Server program terminated\n");
    
    return 0;
}