Merge branch 'fix/reformat'
This commit is contained in:
commit
b20871ae60
11
client1.c
11
client1.c
@ -1,8 +1,3 @@
|
||||
// client1 - client program for demonstrating data transfer over TCP/IP
|
||||
|
||||
// This program complies with the GNU Coding Standards found here:
|
||||
// https://www.gnu.org/prep/standards/html_node/index.html
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
@ -92,9 +87,9 @@ int main (int argc, char**argv)
|
||||
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);
|
||||
printf("Bytes transferred: %lld\n", count);
|
||||
printf("Total size: %lld \n", sz);
|
||||
printf("Total duration: %ld.%d \n", delta.tv_sec, delta.tv_usec);
|
||||
close(sfd);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
96
server1.c
96
server1.c
@ -1,9 +1,3 @@
|
||||
/*
|
||||
* ENGR2782 - CN Assignment
|
||||
* Your challenge is to format the following code in a readable manner, anwsering the questions in the comments.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
@ -16,11 +10,15 @@
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <strings.h>
|
||||
|
||||
#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 main(int argc, char** argv)
|
||||
{
|
||||
int sfd, fd;
|
||||
unsigned int length = sizeof(struct sockaddr_in);
|
||||
long int n, m, count = 0;
|
||||
@ -31,22 +29,33 @@ int main(int argc, char** argv){
|
||||
time_t intps;
|
||||
struct tm* tmi;
|
||||
|
||||
|
||||
/* (A) repalce the following line with suitable getopt() functionality */
|
||||
if(argc!=2) {perror("Usage ./a.out <num_port> <file2send>\n");exit(3);}
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
perror("Usage ./a.out <num_port> <file2send>\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);}
|
||||
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);
|
||||
|
||||
@ -60,7 +69,12 @@ int main(int argc, char** argv){
|
||||
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);}
|
||||
if (bind(sfd, (struct sockaddr*) &sock_serv, l) == -1)
|
||||
{
|
||||
perror("bind fail");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
bzero(buffer,BUFFERT);
|
||||
listen(sfd,BACKLOG);
|
||||
|
||||
@ -71,34 +85,72 @@ int main(int argc, char** argv){
|
||||
/* 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);}
|
||||
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);}
|
||||
|
||||
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;
|
||||
|
||||
while(n)
|
||||
{
|
||||
if (n==-1)
|
||||
{
|
||||
perror("recv fail");
|
||||
exit(5);
|
||||
}
|
||||
|
||||
if ((m=write(fd,buffer,n))==-1)
|
||||
{
|
||||
perror("write fail");
|
||||
exit(6);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user