-
Notifications
You must be signed in to change notification settings - Fork 0
/
Netcpy.java
85 lines (77 loc) · 2.34 KB
/
Netcpy.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
import java.net.*;
import java.io.*;
import java.nio.file.*;
public class Netcpy{
public static void main(String[] args) {
File temp = new File(args[0]);
if (temp.exists()) {
System.out.println("File " + args[0] + " exists!");
System.out.println("****File details: ****");
System.out.println("File Name: " + temp.getName());
System.out.println("Absolute File Name: " + temp.getAbsoluteFile());
System.out.println("Reading File...");
try{
FileHandle readFH = new FileHandle(temp);
Path outputPath = Paths.get("save/output.mp4");
Files.createFile(outputPath);
FileOutputStream fOS = new FileOutputStream(new File("save/output.mp4"),true);
while(!readFH.getIsReadComplete()){
byte[] bfn = readFH.read();
//convert into udp packets and start sending operation
//check if all the packets reach the destination
//once they reach then start reading again and sending again until
//all the file is transfered to the destination.
fOS.write(bfn);
}
fOS.close();
}catch(FileNotFoundException e){
System.out.println("File not found"+e.getMessage());
}catch(IOException e){
System.out.println("IO: "+e.getMessage());
}
}else{
System.out.println("File "+args[0]+" doesn't exists!");
}
/*String src_file_name = args[0];
String des_file_name = args[1];
String comp_ip = args[2];
String comp_port = args[3];
DatagramSocket socket = null;
try
{
//Read file first
socket = new DatagramSocket();
//get bytes
byte[] m = msg.getBytes();
InetAddress server_ip = InetAddress.getByName("localhost");
int server_port = 6789;
DatagramPacket request = new DatagramPacket(m, m.length, server_ip, server_port);
//convert to packets
//send to server
//find acknowledgement
socket.send(request);
System.out.println("message sent to server....");
byte[] buffer = new byte[1000];
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
socket.receive(reply);
System.out.println("message from server: " + new String(reply.getData()));
}
catch(SocketException e)
{
System.out.println(e.getMessage());
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.err.println("Unexpected IO ERROR: " + e);
}
finally
{
if(socket != null)
socket.close();
}*/
}
}