- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTFTPClient.java
More file actions
Latest commit
171 lines (142 loc) · 5.43 KB
/
Copy pathTFTPClient.java
File metadata and controls
171 lines (142 loc) · 5.43 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
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.net.DatagramPacket;
importjava.net.DatagramSocket;
importjava.net.SocketTimeoutException;
importjava.net.UnknownHostException;
importjava.util.*;
/**
* Created by mandeep on 8/9/14.
*/
publicclassTFTPClient {
staticPacketsp;
staticStringserver;
publicstaticvoidmain(String[] args) throwsIOException, InterruptedException {
Scannerin = newScanner(System.in);
try {
while (true) {
// Prompt input.
System.out.print("myTftp >");
Stringcommand = in.nextLine();
processCommand(command);
}
} catch (Exceptionexp) {
System.out.println(exp.getMessage());
}
}
// Print a message with preceding prompt.
publicstaticvoidprintPromptMessage(Stringmessage) {
System.out.println("myTftp > " + message);
}
privatestaticvoidprocessCommand(Stringcommand) throwsIOException {
StringTokenizerst = newStringTokenizer(command);
// When enter pressed without any command.
if (st.countTokens() == 0) return;
// Performing actions acc. to command.
Stringcomm = st.nextToken();
if (comm.equalsIgnoreCase("get")) {
processGet(st);
} elseif (comm.equalsIgnoreCase("connect")) {
processConnect(st);
}
elseif (comm.equalsIgnoreCase("quit")) System.exit(0);
elseprintPromptMessage("Invalid command");
}
// Check for server name and attempt to connect to it.
privatestaticvoidprocessConnect(StringTokenizerst) {
try {
// Check for server name.
if (!st.hasMoreTokens()) {
printPromptMessage("Server name not given.");
return;
}
p = newPackets(st.nextToken());
System.out.println("Connected to " + p.TFTPServerIP);
} catch (UnknownHostExceptionunk) {
printPromptMessage("Unknown host.");
return;
}
}
privatestaticvoidprocessGet(StringTokenizerst) throwsIOException {
// Check if connection has been established.
if (p == null) {
printPromptMessage("Connect to server with: connect <server>");
return;
}
if (st.hasMoreTokens()){
while (st.hasMoreTokens())
getFile(st.nextToken().trim());
}
else {
printPromptMessage("get file1 file2 ....");
return;
}
}
privatestaticvoidgetFile(StringfileName) throwsIOException {
if (fileName.isEmpty()) {
printPromptMessage("get file1 file2 ...");
return;
}
DatagramSocketclientSocket = newDatagramSocket();
DatagramPacketsendPacket = p.getReadPacket(fileName);
clientSocket.send(sendPacket);
DatagramPacketreceivePacket;
StringdirPath = "./mhs1841";
Filedir = newFile(dirPath);
if (!dir.exists()) dir.mkdir();
Filef = newFile(fileName);
OutputStreamos = newFileOutputStream(dir + "/" + f.getName(), true);
longsize = 0;
longstart = System.currentTimeMillis();
try {
do {
receivePacket = receivePacket(clientSocket);
intopcode = receivePacket.getData()[1];
// Data packet with opcode = 3
if (opcode == 3) {
size = processDataPacket(clientSocket, receivePacket, os, size, start, f.getName());
}
// Error message
elseif (opcode == 5) {
printPromptMessage(newString(receivePacket.getData(), 4, receivePacket.getLength() - 2));
// Delete partial file.
newFile(dir + "/" + f.getName()).delete();
break;
}
} while (receivePacket.getLength() == 516);
os.close();
clientSocket.close();
} catch (SocketTimeoutExceptionstExp) {
printPromptMessage("Timeout.");
newFile(dir + "/" + f.getName()).delete();
}
}
privatestaticlongprocessDataPacket(DatagramSocketclientSocket,
DatagramPacketreceivePacket, OutputStreamos,
longsize, longstart, Stringfile) throwsIOException {
byte[] data = receivePacket.getData();
os.write(data, 4, receivePacket.getLength() - 4);
System.out.print("\r");
// Get ACK for received packet.
DatagramPacketack = p.getACK(receivePacket);
size += receivePacket.getLength() - 4;
// Write status.
System.out.print("Downloaded " + size + " bytes in " +
(System.currentTimeMillis() - start) / 1000 +
" seconds");
// Send ack
clientSocket.send(ack);
if (receivePacket.getLength() < 516)
System.out.println("\n" + file +" done." );
returnsize;
}
privatestaticDatagramPacketreceivePacket(DatagramSocketclientSocket) throwsIOException {
byte[] receiveData = newbyte[516];
DatagramPacketreceivePacket = newDatagramPacket(receiveData, receiveData.length);
clientSocket.setSoTimeout(10000);
clientSocket.receive(receivePacket);
returnreceivePacket;
}
}