-
Notifications
You must be signed in to change notification settings - Fork 0
/
FxClient.java
111 lines (73 loc) · 2.68 KB
/
FxClient.java
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
import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
public class FxClient{
public static void main(String[] args) throws Exception{
String command = args[0];
String fileName = args[1];
String header = null;
StringTokenizer strk = null;
Socket connectionToServer = new Socket("localhost", 80);
InputStream in = connectionToServer.getInputStream();
OutputStream out = connectionToServer.getOutputStream();
BufferedReader headerReader = new BufferedReader(new InputStreamReader(in));
BufferedWriter headerWriter = new BufferedWriter(new OutputStreamWriter(out));
DataInputStream dataIn = new DataInputStream(in);
DataOutputStream dataOut = new DataOutputStream(out);
if(command.equals("d")){
header = "download " + fileName + "\n";
headerWriter.write(header, 0, header.length());
headerWriter.flush();
header = headerReader.readLine();
if(header.equals("NOT FOUND")){
System.out.println("We're extremely sorry, the file you specified is not available!");
}else{
strk = new StringTokenizer(header, " ");
String status = strk.nextToken();
if(status.equals("OK")){
int size = Integer.parseInt(strk.nextToken());
byte[] space = new byte[size];
dataIn.readFully(space);
FileOutputStream fileOut = new FileOutputStream("ClientShare/" + fileName);
fileOut.write(space, 0, size);
fileOut.close();
}else{
System.out.println("You're not connected to the right Server!");
}
}
}else if(command.equals("u")){
try {
FileInputStream fileIn = new FileInputStream("ClientShare/"+ fileName);
int fileSize = fileIn.available();
header = "upload " + fileName + " " + fileSize +"\n";
headerWriter.write(header, 0, header.length());
headerWriter.flush();
byte[] bytes = new byte[fileSize];
fileIn.read(bytes);
fileIn.close();
dataOut.write(bytes, 0, fileSize);
header = headerReader.readLine();
if(header.equals("OK")) {
System.out.println("Upload Completed!");
} else {
System.out.println("File Already Exists!");
}
} catch(Exception e) {
System.out.println("File does not exist in local system!");
}
} else if(command.equals("del")) {
header = "delete " + fileName + "\n";
headerWriter.write(header, 0, header.length());
headerWriter.flush();
header = headerReader.readLine();
if(header.equals("NOT FOUND")) {
System.out.println("No such file exists!");
} else {
System.out.println("Deletion Complete!");
}
} else{
//To do
}
connectionToServer.close();
}
}