commit 5bb16cf20a6ddb37f3e3cb856af55d8c537a7e54 Author: Thomas Wade Date: Wed Oct 31 21:44:42 2018 +1030 Initial commit diff --git a/client1.c b/client1.c new file mode 100644 index 0000000..b332d05 --- /dev/null +++ b/client1.c @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BUFFERT 512 + +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 int n; + int l=sizeof(struct sockaddr_in); + struct stat buffer; + if (argc != 4){ + printf("Error usage : %s \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; + } + if (stat(argv[3],&buffer)==-1){ + perror("stat fail"); + return EXIT_FAILURE; + } + else + sz=buffer.st_size; + bzero(&buf,BUFFERT); + if(connect(sfd,(struct sockaddr*)&sock_serv,l)==-1){ + perror("conexion error\n"); + exit (3); + } + 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; + bzero(buf,BUFFERT); + n=read(fd,buf,BUFFERT); + } + m=sendto(sfd,buf,0,0,(struct sockaddr*)&sock_serv,l); + gettimeofday(&stop,NULL); + duration(&start,&stop,&delta); + printf("Nombre d'octets transférés : %lld\n",count); + printf("Sur une taille total de : %lld \n",sz); + printf("Pour une durée total de : %ld.%d \n",delta.tv_sec,delta.tv_usec); + close(sfd); + return EXIT_SUCCESS; +} + +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; +} + +int create_client_socket (int port, char* ipaddr){ + int l; + int sfd; + sfd = socket(PF_INET,SOCK_STREAM,0); + if (sfd == -1){ + perror("socket fail"); + return EXIT_FAILURE; + } + 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; +} \ No newline at end of file diff --git a/server1.c b/server1.c new file mode 100644 index 0000000..e690f62 --- /dev/null +++ b/server1.c @@ -0,0 +1,104 @@ +/* + * ENGR2782 - CN Assignment + * Your challenge is to format the following code in a readable manner, anwsering the questions in the comments. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define BUFFERT 512 +#define BACKLOG 1 +int create_server_socket (int port); +struct sockaddr_in sock_serv,sock_clt; +int main(int argc, char** argv){ + int sfd, fd; + unsigned int length = sizeof(struct sockaddr_in); + long int n, m, count = 0; + unsigned int nsid; + ushort clt_port; + char buffer[BUFFERT], filename[256]; + char dst[INET_ADDRSTRLEN]; + time_t intps; + struct tm* tmi; + + + /* (A) repalce the following line with suitable getopt() functionality */ + if(argc!=2) {perror("Usage ./a.out \n");exit(3);} + + + int l; + int yes=1; + + + /* The following function call should use; Internet version 4 protocols with a sequenced, reliable, two-way connection based byte streams */ + sfd = socket( /* Q) What goes here? */ , /* Q) What goes here? */ , 0); + /* See; man socket */ + + + if (sfd == -1) {perror("socket fail");return EXIT_SUCCESS;} + if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(int)) == -1 ) {perror("setsockopt erreur");exit(5);} + l = sizeof(struct sockaddr_in); + bzero(&sock_serv,l); + + /* The following assignment should use; Internet version 4 protocols constant */ + sock_serv.sin_family=/* Q) What goes here? */; + /* See; man socket */ + + sock_serv.sin_port = htons(atoi(argv[1])); + + /* Set the IP address to allow connections on any network interface on the server */ + sock_serv.sin_addr.s_addr = htonl(/* Q) What goes here? */); + /* See; man 4 ip */ + + if (bind(sfd,(struct sockaddr*)&sock_serv,l) == -1) {perror("bind fail"); exit(EXIT_FAILURE);} + bzero(buffer,BUFFERT); + listen(sfd,BACKLOG); + + /* accept() takes "struct sockaddr_in" and returns non-negative integer that is a descriptor for the accepted socket. + * The missing arguement in the line below will need to have the *address* of the variable required, as the value is updated when the socket is update. eg, &variable + */ + nsid = accept(sfd, (struct sockaddr*)&sock_clt, /* Q) What goes here? */); + /* See; man 2 accept */ + + + if (nsid == -1) {perror("accept fail"); return EXIT_FAILURE;} + else { + if(inet_ntop(AF_INET,&sock_clt.sin_addr,dst,INET_ADDRSTRLEN)==NULL) {perror("erreur socket");exit (4);} + clt_port=ntohs(sock_clt.sin_port); + printf("connection : %s:%d\n",dst,clt_port); + bzero(filename,256); + intps = time(NULL); + tmi = localtime(&intps); + bzero(filename,256); + sprintf(filename,"saved-file.%d.%d.%d.%d.%d.%d", tmi->tm_mday, tmi->tm_mon+1, 1900+tmi->tm_year, tmi->tm_hour, tmi->tm_min, tmi->tm_sec); + printf("Creating the copied output file : %s\n", filename); + if ((fd = open(filename, O_CREAT|O_WRONLY, 0600)) == -1) {perror("open fail"); exit (3);} + bzero(buffer,BUFFERT); + n=recv(nsid,buffer,BUFFERT,0); + while(n) { + if(n==-1) {perror("recv fail");exit(5);} + if((m=write(fd,buffer,n))==-1) {perror("write fail");exit (6);} + count=count+m; + bzero(buffer,BUFFERT); + n=recv(nsid,buffer,BUFFERT,0); + } + close(sfd); + close(fd); + } + close(nsid); + printf("End of transmission with %s.%d\n",dst,clt_port); + + /* Select the appropriate variable in the following function call argument */ + printf("Number of octets received : %ld \n", /* Q) What goes here? */); + return EXIT_SUCCESS; +}