-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_client.c
182 lines (153 loc) · 4.15 KB
/
2_client.c
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
#include<sys/ioctl.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<net/if_arp.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
#include<stdio.h>
#include<stdbool.h>
#include<signal.h>
#define FNAME file1
#define PORT 8010
#define BUFSIZE 128
#define MAX_CMDSIZE 4
#define GET 1
#define PUT 2
#define LIST 3
#define EXIT 4
int getCommand(char* S)
{
if(strcmp(S,"GET")==0)
return GET;
else if(strcmp(S,"PUT")==0)
return PUT;
else if(strcmp(S,"LIST")==0)
return LIST;
else if(strcmp(S,"EXIT")==0)
return EXIT;
}
void handler(int sig)
{
printf("\nFTP Command Usage: \nGET <press 1> -to recieve file from server\n");
printf("PUT <press 2> -to store file on server\n");
printf("LIST <press 3> -to see the list of stored files at server\n");
printf("EXIT <press 4> -to quit to terminal\n");
printf("\nNote: Please use FTP commands instead of terminating :)\n");
printf("\nEnter command : ");
fflush(stdout);
}
int main(int argc, char **argv)
{
signal(SIGINT, handler);
int sockfd, fd, n, size,count=0;
long int size1,size2;
char buf[BUFSIZE], fname[50];
struct sockaddr_in servaddr;
struct stat stat_buf;
if (argc != 2) {
printf("Usage: %s server_address\n", argv[0]);
exit(1);
}
//****************** Connection Back-end code START *****************************//
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
exit(1);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
exit(1);
if (connect(sockfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0)
perror("Not working :(\n"),
exit(1);
printf("connection established\n");
//****************** Connection Back-end code END *****************************//
printf("FTP Command Usage: \nGET <press 1> -to recieve file from server\n");
printf("PUT <press 2> -to store file on server\n");
printf("LIST <press 3> -to see the list of stored files at server\n");
printf("EXIT <press 4> -to quit to terminal\n");
bool active=1;
while(active)
{
printf("\nEnter command : ");
int cmd;
scanf("%d",&cmd);
switch(cmd)
{
case GET:
send(sockfd,&cmd, sizeof(cmd) ,0); //sizeof integer 4
printf("File name : ");
scanf("%s",fname);
send(sockfd,fname,sizeof(fname),0);
//printf("%s",fname);
fd=open(strcat(fname,"_fromserver"),O_WRONLY|O_CREAT,S_IRWXU);
fstat(fd, &stat_buf);
size = stat_buf.st_size;
if(size==0)
{
printf("The file is non-existing or empty :(. Press 3 to see list\n");
break;
}
while ( (n = read(sockfd, buf, BUFSIZE-1)) > 0)
{
buf[n] = '\0';
printf("%s\n",buf);
write(fd,buf,n);
if( n < BUFSIZE-2)
break;
}
printf("file receiving completed, find your file in folder\n");
close(fd);
break;
case PUT:
printf("File name : ");
scanf("%s",fname);
fd=open(fname,O_RDONLY,S_IRUSR);
fstat(fd, &stat_buf);
size = stat_buf.st_size;
if(size==0)
{
printf("The file is non-existing or empty :(\n");
break;
}
send(sockfd,&cmd, sizeof(cmd) ,0); //sizeof integer 4
send(sockfd,fname,sizeof(fname),0);
//printf("%s",fname);
printf("sending file %s : size is %d\n", fname,size);
printf("\nopened file\n");
while ( (n = read(fd, buf, BUFSIZE-1)) > 0)
{
buf[n] = '\0';
printf("%s\n",buf);
write(sockfd,buf,n);
}
printf("\nfile transfer completed \n");
close(fd);
break;
case LIST:
send(sockfd,&cmd,sizeof(cmd),0);
printf("following is the list of files and directories on server\n");
while ( (n = read(sockfd, buf, BUFSIZE-1)) > 0)
{
buf[n] = '\0';
printf("%s\n",buf);
if( n < BUFSIZE-2)
break;
}
break;
case EXIT:
send(sockfd,&cmd,sizeof(cmd),0);
active = 0;
printf("Thanks for using FTP service, visit again :)\n");
break;
default:
printf("Please enter allowed FTP commands :)\n");
}//switch end
}//while end
close(sockfd);
exit(0);
}