Last active
October 16, 2017 10:27
-
-
Save AjayKrP/78f0976375791e981d3da941125a4286 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
// Time function, sockets, htons... file stat | |
#include <sys/time.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <sys/types.h> | |
#include <sys/uio.h> | |
#include <sys/stat.h> | |
// File function and bzero | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <strings.h> | |
/* Size of the buffer used to send the file | |
* in several blocks | |
*/ | |
#define BUFFERT 512 | |
/* Command to generate a test file | |
* dd if = / dev / urandom of = file count = 8 | |
*/ | |
int duration (struct timeval *start,struct timeval *stop, struct timeval *delta); | |
int create_client_socket (int port, char* ipaddr); | |
struct sockaddr_in sock_serv; | |
int main (int argc, char**argv){ | |
struct timeval start, stop, delta; | |
int sfd,fd; | |
char buf[BUFFERT]; | |
off_t count=0, m,sz;//long | |
long int n; | |
int l=sizeof(struct sockaddr_in); | |
struct stat buffer; | |
if (argc != 4){ | |
printf("Error usage : %s <ip_serv> <port_serv> <filename>\n",argv[0]); | |
return EXIT_FAILURE; | |
} | |
sfd=create_client_socket(atoi(argv[2]), argv[1]); | |
if ((fd = open(argv[3],O_RDONLY))==-1){ | |
perror("open fail"); | |
return EXIT_FAILURE; | |
} | |
//file size | |
if (stat(argv[3],&buffer)==-1){ | |
perror("stat fail"); | |
return EXIT_FAILURE; | |
} | |
else | |
sz=buffer.st_size; | |
// preparing the sending | |
bzero(&buf,BUFFERT); | |
gettimeofday(&start,NULL); | |
n=read(fd,buf,BUFFERT); | |
while(n){ | |
if(n==-1){ | |
perror("read fails"); | |
return EXIT_FAILURE; | |
} | |
m=sendto(sfd,buf,n,0,(struct sockaddr*)&sock_serv,l); | |
if(m==-1){ | |
perror("send error"); | |
return EXIT_FAILURE; | |
} | |
count+=m; | |
//fprintf(stdout,"----\n%s\n----\n",buf); | |
bzero(buf,BUFFERT); | |
n=read(fd,buf,BUFFERT); | |
} | |
// read just returned 0: end of file | |
// to unlock the serv | |
m=sendto(sfd,buf,0,0,(struct sockaddr*)&sock_serv,l); | |
gettimeofday(&stop,NULL); | |
duration(&start,&stop,&delta); | |
printf ("Number of bytes transferred:% lld \n", count); | |
printf ("On a total size of:% lld \n", sz); | |
printf ("For a total duration of:% ld.%d \n",delta.tv_sec,delta.tv_usec); | |
close(sfd); | |
close(fd); | |
return EXIT_SUCCESS; | |
} | |
/* Function to calculate the duration of the sending */ | |
int duration (struct timeval *start,struct timeval *stop,struct timeval *delta) | |
{ | |
suseconds_t microstart, microstop, microdelta; | |
microstart = (suseconds_t) (100000*(start->tv_sec))+ start->tv_usec; | |
microstop = (suseconds_t) (100000*(stop->tv_sec))+ stop->tv_usec; | |
microdelta = microstop - microstart; | |
delta->tv_usec = microdelta%100000; | |
delta->tv_sec = (time_t)(microdelta/100000); | |
if((*delta).tv_sec < 0 || (*delta).tv_usec < 0) | |
return -1; | |
else | |
return 0; | |
} | |
/* Function to create a socket | |
* Returns a file descriptor | |
*/ | |
int create_client_socket (int port, char* ipaddr){ | |
int l; | |
int sfd; | |
sfd = socket(AF_INET,SOCK_DGRAM,0); | |
if (sfd == -1){ | |
perror("socket fail"); | |
return EXIT_FAILURE; | |
} | |
// prepare destination socket address | |
l=sizeof(struct sockaddr_in); | |
bzero(&sock_serv,l); | |
sock_serv.sin_family=AF_INET; | |
sock_serv.sin_port=htons(port); | |
if (inet_pton(AF_INET,ipaddr,&sock_serv.sin_addr)==0){ | |
printf("Invalid IP adress\n"); | |
return EXIT_FAILURE; | |
} | |
return sfd; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment