-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_ftp.c
More file actions
104 lines (88 loc) · 2.96 KB
/
Copy pathclient_ftp.c
File metadata and controls
104 lines (88 loc) · 2.96 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
/*
* Function: ftp client
*
* Author : Daniel Lai
* Created On: 2014-02-21
*/
#include <client_ftp.h>
int main(int argc, char* argv[])
{
// initialization begins
struct sockaddr_in skin_server;
int sfd_client, x;
size_t size_sockaddr = sizeof(struct sockaddr), size_packet = sizeof(struct packet);
short int connection_id;
struct packet* chp = (struct packet*) malloc(size_packet); // client host packet
set0(chp);
struct packet* data; // network data packet
if((x = sfd_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
er("socket()", x);
memset((char*) &skin_server, 0, sizeof(struct sockaddr_in));
skin_server.sin_family = AF_INET;
skin_server.sin_addr.s_addr = inet_addr(IPSERVER);
skin_server.sin_port = htons(PORTSERVER);
if((x = connect(sfd_client, (struct sockaddr*) &skin_server, size_sockaddr)) < 0)
er("connect()", x);
printf(ID "FTP Client started up. Attempting communication with server @ %s:%d...\n\n", IPSERVER, PORTSERVER);
printf(ID "Connect command channel success! Please build the data transfering channel with server by command 'prt [port_num]!'\n");
connect_flag = ISCONNECT;
// initialization ends
struct command* cmd;
char lpwd[LENBUFFER], pwd[LENBUFFER];
char userinput[LENBUFFER];
while(1)
{
printf("\t> ");
fgets(userinput, LENBUFFER, stdin);
cmd = userinputtocommand(userinput);
if(!cmd)
continue;
printcommand(cmd);
// check connect status
if( connect_flag == ISNOTCONNECT )
{
printf(ID "Please connect server first.\n");
continue;
}
// check prt flag
if(cmd->id != 1 && DATAPORTNOTBUILT == prt_flag)
{
printf(ID "Please build the data transfering channel first! command 'prt [port_num]'\n");
continue;
}
else if(cmd->id == 1 && DATAPORTBUILT == prt_flag)
{
printf(ID "Data transfering channel has been build!\n");
continue;
}
switch(cmd->id)
{
case GET:
if(cmd->arguments)
command_get(chp, data, sfd_client, cmd->argname);
else
fprintf(stderr, "No path to file given.\n");
break;
case PRT:
if(cmd->arguments)
command_prt(chp, data, sfd_client, cmd->argname);
else
fprintf(stderr, "No portnum given.\n");
break;
case DIR_:
command_dir(chp, data, sfd_client);
break;
case BYE:
command_bye(chp, data, sfd_client);
return 0;
break;
default:
// display error
break;
}
}
close(sfd_client);
printf(ID "Done.\n");
fflush(stdout);
return 0;
}