-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftp_client.java
More file actions
129 lines (103 loc) · 4.5 KB
/
ftp_client.java
File metadata and controls
129 lines (103 loc) · 4.5 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
import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
class Cli_sender extends Thread{
Socket connecting_socket;
public Cli_sender(Socket cs){
this.connecting_socket=cs;
}
@Override
public void run(){
while(true){
try {
DataOutputStream to_server=new DataOutputStream(connecting_socket.getOutputStream());
DataInputStream from_server=new DataInputStream(connecting_socket.getInputStream());
BufferedReader u_inp=new BufferedReader(new InputStreamReader(System.in));
String uinp=u_inp.readLine();
System.out.println(uinp.substring(0, 4));
String command="";
String filename=uinp;
int fn_index=0;
if(uinp.substring(0, 4).equals("send")){
fn_index=5;
command="send";
}
else if(uinp.substring(0, 3).equals("get")){
fn_index=4;
command="get";
}
else if(filename.equals("list")){
}
else{
System.out.println("wrong command input ,use \'get\' \'send\' or \'list\' ,get/send filename+end ,list is by itself");
}
File file=new File(filename.substring(fn_index,filename.length()-1));
if(command.equals("send")){
FileInputStream fin=new FileInputStream(file);
System.out.println("filelengtsis: "+Long.toString(file.length()));
to_server.writeBytes(filename);
to_server.writeLong(file.length());
//to_server.flush();
//to_server.writeBytes(uinp);
//
int bytes;
byte[] buffer=new byte[4*1024];
while((bytes=fin.read(buffer))!=-1){
to_server.write(buffer,0,bytes);
to_server.flush();
}
System.out.println("file sent!");
fin.close();
}
else if(command.equals("get")){
byte[] buffer=new byte[4*1024];
to_server.writeBytes(filename);
long size=from_server.readLong();
System.out.println("tying to get this work:"+size);
FileOutputStream foup=new FileOutputStream(file);
System.out.println("here::"+filename);
int bytes=0;
System.out.println("size is:"+Long.toString(size));
while(size>0&&(bytes = from_server.read(
buffer, 0,
(int)Math.min(buffer.length, size)))
!= -1){
foup.write(buffer,0,bytes);
size-=bytes;
System.out.println("remaining:"+Long.toString(size/1000)+"kb");
}
foup.close();
System.out.println("done");
}
else if(filename.equals("list")){
to_server.writeBytes(filename);
String content="";
int byt=0;
System.out.println("listing now");
while((byt=from_server.read())!=-1){
content+=(char)byt;
if((char)byt==';')break;
//System.out.println(content);
}
System.out.println("and voila now");
System.out.print(content);
}
}
catch (Exception e) {
// TODO: handle exception
}
}
}
}
class ftp_client{
public static void main(String argv[])throws Exception{
Socket cli_socket=new Socket(argv[0],6969);
if(cli_socket.isConnected()){
System.out.print("connected to the server\n");
}
Cli_sender sender=new Cli_sender(cli_socket);
sender.start();
}
}