-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_ftp.c
More file actions
executable file
·190 lines (174 loc) · 6.86 KB
/
client_ftp.c
File metadata and controls
executable file
·190 lines (174 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Program: client_base.c
* Author: Tianyu Song
* TUT_NUMBER 20126169
* Date: Sept 2013
*
* Objective: Show how to use a tcp socket
* on a client program running on
* Sun/ Solaris ; this program transmits
* a message to a server and receives * an acknowledge each time. A delay of
* one second is introduced between messages.
*
* Options to compile on Solaris: * ===> gcc client_ftp.c -lsocket -lnsl -o client_ftp * Options to compile on Linux:
* ===> gcc client_ftp.c -lrt -lnsl -o client_ftp
*
* Execution: 3 parameters: program_name server_name server port_number
* =========> client_ftp server_machine 5161
*
*
*/
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <time.h>
/*
* following functions are used in this program
*
* socket, gethostbyname, connect, * read, write, close.
*/
FILE *fptr; /* pointer to file descriptor */
struct timespec time1 , time2 ,temp; /* time_t tv_sec ; seconds long tv_nsec ; nanoseconds */
struct hostent *hp,*gethostbyname(); /* structure to associate the
server name and its address */
struct hostent hpstruct; /* structure to access the server address*/
struct sockaddr_in server2; /* structure used to assign a name to a socket sccording internet format*/
size_t length; /* #octets in structure sockadr_in */
char local_filename[20]; /* save the local_file_name which is input by user */
char remote_filename[20]; /* save the remote_file_name which is input bu user */
char fileContentBuf[10000] ; /*read the file buffer*/
int file_line = 0 , file_size = 0 ; /*file file_line file_size*/
int sock2; /* socket descriptor */
double delay ;
int inputFileName(){
printf("myftp> ");
if(scanf("put %s",local_filename) == EOF || scanf("%s",remote_filename) == EOF )
{
return 0 ;
}else{
printf("\nftp command=> put local file=> %s remote file=> %s\n",local_filename,remote_filename);
return 1;
}
}
int getConnection(){
/* 5. Try a connection with the server
*/
printf("\n=================== Start ======================\n") ;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID , &time1 ) ; /* read the time1 nanosec */
if (connect(sock2, (struct sockaddr *)&server2, sizeof(server2)) < 0)
{
return 0;
} else
return 1;
}
int getSocketBase(int argc , char *argv[]){
/*
* 1. Validation of the parameters read on the command file_line
*/ if (argc != 3)
{
return 0;
}
/*
* 2. Socket creation parameters:
* - AF_INET is the internet format * - SOCK_STREAM specifies a TCP type socket * - 0 specifies to use the default protocol.
*/ sock2 = socket(AF_INET, SOCK_STREAM, 0);
if (sock2 < 0)
{
perror("Error in creating a TCP socket");
return 0;
}
/*
* 3. Get a pointer to the network server address structure.
* The server name is taken from the command file_line (argv[1]):
* ex. "sunens.uqac.ca"
*/ server2.sin_family = AF_INET; /* internet format*/
hp = gethostbyname(argv[1]);
if (hp == 0)
{
fprintf(stderr, "%s: unknown host ===>", argv[1]);
return 0;
}
hpstruct = *hp; /* save hostent data */
/*
* 4. Fill in the remote address part
*/
server2.sin_family = hpstruct.h_addrtype;
server2.sin_port = htons(atoi(argv[2])); /* char port # ==>integer port #
====> network 16 bits format */
server2.sin_addr = * ((struct in_addr *) hpstruct.h_addr);
return 1;
}
int transferFileName(){
if(write(sock2,local_filename ,strlen(local_filename)) < 0)
return 0 ;
else
return 1 ;
}
int openFile(){
if( (fptr = fopen(local_filename,"r") ) == NULL)
{
perror("error opening this file") ;
return 0 ;
}
return 1 ;
}
int sendFile(){ if(openFile()){ /* start read the file and to transfer the file*/
if(write(sock2 ,remote_filename,strlen(remote_filename) ) < 0 )
{
perror("ERROR in ransmission the remote_filename") ;
return 0 ;
}
while(fgets(fileContentBuf,1000,fptr))
{
file_line++ ;
file_size+=strlen(fileContentBuf) ;
if(write(sock2, fileContentBuf,strlen(fileContentBuf)) < 0)
{
perror("ERROR in the ransmission the file") ;
return 0 ;
}
}
}
fclose(fptr) ; return 1;
}
int main(int argc,char *argv[])
{
if( !getSocketBase(argc,argv) )
{
perror("Call the program : client_base server_name port_number\n");
return 0 ;
}
if( !getConnection() ){
perror("Error at connect time\n");
return 0 ;
}
if( !inputFileName() ){
perror("error input format : put location_file_name remote_file_name\n") ;
return 0 ;
}
if( !sendFile() ) {
perror("ERROR in ransmission the File\n") ;
return 0 ;
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2) ; /*read the timer2 in nanosec */
if((time2.tv_nsec - time1.tv_nsec) < 0)
{
temp.tv_sec = time2.tv_sec - time1.tv_sec - 1 ;
temp.tv_nsec = 1000000000 + time2.tv_nsec - time1.tv_nsec ;
}else{
temp.tv_sec = time2.tv_sec - time1.tv_sec ;
temp.tv_nsec = time2.tv_nsec - time1.tv_nsec ;
}
delay = (double) (temp.tv_sec * 1000000 ) + (temp.tv_nsec/1000) ; /* delay in microsec */
delay = (double) delay/1000 ;
printf("\nFile %s had %d file_lines and a total of %d octets\n",local_filename,file_line , file_size) ;
printf("Transfer rate : %9.3f MO/sec \t Transfer delay:%9.3f msec\n",(double)((file_size*0.9765)/(1024*delay)) ,delay) ;
close(sock2);
puts("\nEnd of client program\t\tBy: 20126169_Tianyu Song\n");
printf("\n=================== Done ======================\n\n") ;
return 0; /* end of client program */
}