Comply with GNU Coding Standards
This commit is contained in:
parent
5bb16cf20a
commit
59c594c5c3
61
client1.c
61
client1.c
@ -1,3 +1,8 @@
|
||||
// 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>
|
||||
@ -12,12 +17,15 @@
|
||||
|
||||
#define BUFFERT 512
|
||||
|
||||
int duration (struct timeval *start,struct timeval *stop, struct timeval *delta);
|
||||
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){
|
||||
int main (int argc, char**argv)
|
||||
{
|
||||
struct timeval start, stop, delta;
|
||||
int sfd, fd;
|
||||
char buf[BUFFERT];
|
||||
@ -25,43 +33,62 @@ int main (int argc, char**argv){
|
||||
long int n;
|
||||
int l = sizeof(struct sockaddr_in);
|
||||
struct stat buffer;
|
||||
if (argc != 4){
|
||||
|
||||
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){
|
||||
if ((fd = open(argv[3], O_RDONLY)) == -1)
|
||||
{
|
||||
perror("open fail");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (stat(argv[3],&buffer)==-1){
|
||||
|
||||
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){
|
||||
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){
|
||||
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){
|
||||
|
||||
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);
|
||||
@ -72,7 +99,8 @@ int main (int argc, char**argv){
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int duration (struct timeval *start,struct timeval *stop,struct timeval *delta)
|
||||
int duration (struct timeval *start, struct timeval *stop,
|
||||
struct timeval *delta)
|
||||
{
|
||||
suseconds_t microstart, microstop, microdelta;
|
||||
|
||||
@ -81,20 +109,28 @@ int duration (struct timeval *start,struct timeval *stop,struct timeval *delta)
|
||||
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 create_client_socket (int port, char* ipaddr)
|
||||
{
|
||||
int l;
|
||||
int sfd;
|
||||
sfd = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (sfd == -1){
|
||||
if (sfd == -1)
|
||||
{
|
||||
perror("socket fail");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
l = sizeof(struct sockaddr_in);
|
||||
bzero(&sock_serv, l);
|
||||
sock_serv.sin_family=AF_INET;
|
||||
@ -103,5 +139,6 @@ int create_client_socket (int port, char* ipaddr){
|
||||
printf("Invalid IP adress\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return sfd;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user