-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUdp_server.java
354 lines (261 loc) · 10.7 KB
/
Udp_server.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;
public class Udp_server {
static String link="";
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int op=0;
BufferedReader q=new BufferedReader(new InputStreamReader(System.in));
while(op!=5)
{
System.out.println("Please choose the following options/n 1)cd\n 2)ls \n 3)put \n 4)get\n5)exit");
String s1=q.readLine();
op=Integer.parseInt(s1);
switch(op)
{
case 1:
{
cd();
break;
}
case 2:
{
ls();
break;
}
case 3:
{
put();
break;
}
case 4:
{
get();
break;
}
}
}
}
public static void cd() throws IOException
{
DatagramSocket ds = new DatagramSocket(1234);
byte[] receive = new byte[65535];
DatagramPacket DpReceive = null;
DpReceive = new DatagramPacket(receive, receive.length);
ds.receive(DpReceive);
String v= data(receive).toString();
System.out.println(v);
File dir = new File(v);
String n;
if(dir.isDirectory()==true)
{
System.setProperty("user.dir", dir.getAbsolutePath());
link=v;
n= new String("changed");
}
else
{
System.out.println(v + " is not a directory.");
n= new String("cant be changed");
}
DatagramSocket ds1 = new DatagramSocket();
InetAddress ip = InetAddress.getLocalHost();
byte buf[] = null;
buf = n.getBytes();
DatagramPacket DpSend =
new DatagramPacket(buf, buf.length, ip, 1240);
ds1.send(DpSend);
ds1.close();
ds.close();
}
public static void ls() throws IOException
{
DatagramSocket ds = new DatagramSocket(1600);
byte[] receive = new byte[65535];
DatagramPacket DpReceive = null;
// Step 2 : create a DatgramPacket to receive the data.
DpReceive = new DatagramPacket(receive, receive.length);
// Step 3 : revieve the data in byte buffer.
ds.receive(DpReceive);
String v= data(receive).toString();
//////
File file = new File(v);
String[] names = file.list();
String send =new String();
for(String name : names)
{
System.out.println(name);
send=send+"##"+name;
}
DatagramSocket ds1 = new DatagramSocket();
InetAddress ip = InetAddress.getLocalHost();
byte buf[] = null;
buf = send.getBytes();
DatagramPacket DpSend =
new DatagramPacket(buf, buf.length, ip, 1240);
ds1.send(DpSend);
ds1.close();
ds.close();
}
public static void put() throws IOException
{
System.out.println("Ready to receive the file!");
// Get the address, port and name of file to send over UDP
final int port = 3005;
String fileName = link;
DatagramSocket ds = new DatagramSocket(3007);
byte[] receive = new byte[65535];
DatagramPacket DpReceive = null;
// Step 2 : create a DatgramPacket to receive the data.
DpReceive = new DatagramPacket(receive, receive.length);
// Step 3 : revieve the data in byte buffer.
ds.receive(DpReceive);
String v= data(receive).toString();
System.out.println(v);
ds.close();
String s2=fileName+v;
receiveAndCreate(port,s2);
}
public static void receiveAndCreate(int port, String fileName) throws IOException {
// Create the socket, set the address and create the file to be sent
DatagramSocket socket = new DatagramSocket(port);
InetAddress address;
File file = new File(fileName);
FileOutputStream outToFile = new FileOutputStream(file);
// Create a flag to indicate the last message
boolean lastMessageFlag = false;
boolean lastMessage = false;
// Store sequence number
int sequenceNumber = 0;
// For each message we will receive
while (!lastMessage) {
// Create byte array for full message and another for file data without header
byte[] message = new byte[1024];
byte[] filebyteArray = new byte[1021];
// Receive packet and retrieve message
DatagramPacket receivedPacket = new DatagramPacket(message, message.length);
socket.receive(receivedPacket);
message = receivedPacket.getData();
// Retrieve the sequence number
sequenceNumber = ((message[0] & 0xff) << 8) + (message[1] & 0xff);
// Retrieve the last message flag
if ((message[2] & 0xff) == 1) {
lastMessageFlag = true;
} else {
lastMessageFlag = false;
}
// Retrieve data from message
for (int i=3; i < 1024 ; i++) {
filebyteArray[i-3] = message[i];
}
// Write the message to the file and print received message
outToFile.write(filebyteArray);
System.out.println("Received: Sequence number = " + sequenceNumber + ", Flag = " + lastMessageFlag);
// Check for last message
if (lastMessageFlag) {
outToFile.close();
socket.close();
lastMessage = false;
break;
}
}
socket.close();
System.out.println("File " + fileName + " has been received.");
}
public static void get() throws IOException
{
final String hostName = "127.0.0.1";
final int port = 3005;
String fileName = "";
Scanner sc= new Scanner(System.in);
fileName="E:/Masters/sem1/Software engineering/1.pdf";
System.out.println("Enter the file format like : .pdf or .txt");
String fileName1 = sc.nextLine();
byte buf[] = null;
buf = fileName1.getBytes();
DatagramSocket ds = new DatagramSocket();
DatagramPacket DpSend =
new DatagramPacket(buf, buf.length,InetAddress.getByName(hostName), 3007);
ds.send(DpSend);
createAndSend(hostName, port, fileName);
}
public static void createAndSend(String hostName, int port, String fileName) throws IOException {
System.out.println("Sending the file");
// Create the socket, set the address and create the file to be sent
DatagramSocket socket = new DatagramSocket();
InetAddress address = InetAddress.getByName(hostName);
File file = new File(fileName);
// Create a byte array to store the filestream
InputStream inFromFile = new FileInputStream(file);
byte[] fileByteArray = new byte[(int)file.length()];
inFromFile.read(fileByteArray);
// Create a flag to indicate the last message and a 16-bit sequence number
int sequenceNumber = 0;
boolean lastMessageFlag = false;
// For each message we will create
for (int i=0; i < fileByteArray.length; i = i+1021 ) {
// Increment sequence number
sequenceNumber += 1;
// Create new message. Set first and second bytes of the message to sequence number
byte[] message = new byte[1024];
message[0] = (byte)(sequenceNumber >> 8);
message[1] = (byte)(sequenceNumber);
// Set flag to 1 if packet is last packet and store it in third byte of header
if ((i+1021) >= fileByteArray.length) {
lastMessageFlag = true;
message[2] = (byte)(1);
} else { // If not last packet, store flag as 0
lastMessageFlag = false;
message[2] = (byte)(0);
}
// Copy the bytes for the message to the message array
if (lastMessageFlag == false) {
for (int j=0; j <= 1020; j++) {
message[j+3] = fileByteArray[i+j];
}
} else if (lastMessageFlag == true) { // If it is the last message
for (int j=0; j < (fileByteArray.length - i) ;j++) {
message[j+3] = fileByteArray[i+j];
}
}
// Send the message
DatagramPacket sendPacket = new DatagramPacket(message, message.length, address, port);
socket.send(sendPacket);
System.out.println("Sent: Sequence number = " + sequenceNumber + ", Flag = " + lastMessageFlag);
// Sleep for 20 milliseconds to avoid sending too quickly
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
socket.close();
System.out.println("File " + fileName + " has been sent");
}
public static StringBuilder data(byte[] a)
{
if (a == null)
return null;
StringBuilder ret = new StringBuilder();
int i = 0;
while (a[i] != 0 )
{
if(a[i]>=0)
{
ret.append((char) a[i]);
}
i++;
}
return ret;
}
}