-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileClient.java
More file actions
executable file
·90 lines (64 loc) · 1.86 KB
/
FileClient.java
File metadata and controls
executable file
·90 lines (64 loc) · 1.86 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
package dchat;
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.File;
public class FileClient extends Thread { // GET : RECEIVED
private Socket sock = null;
private String filename;
private int port;
private InetAddress ip;
public FileClient(String filename, InetAddress ip, int port)
{
this.filename = filename;
this.ip = ip;
this.port = port;
sock = new Socket();
}
public void run()
{
DataInputStream bufRead = null;
DataOutputStream bufWriter = null;
FileInputStream fileIn = null;
SocketAddress endpoint;
byte[] cbuf = new byte[2048];
int nbytes = 0;
File newFile = new File(filename);
if (!newFile.exists() && !newFile.canRead() && !newFile.isFile()) {
return;
}
try {
fileIn = new FileInputStream(newFile);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
endpoint = new InetSocketAddress(ip, port);
sock.connect(endpoint); // connect to a remote address
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bufRead = new DataInputStream(fileIn);
bufWriter = new DataOutputStream(sock.getOutputStream());
while ((nbytes = bufRead.read(cbuf)) != -1) {
bufWriter.write(cbuf, 0, nbytes);
}
bufWriter.flush();
} catch (IOException ex) {
Logger.getLogger(FileServer.class.getName()).log(Level.SEVERE, null, ex);
}
try {
sock.close();
bufWriter.close();
bufRead.close();
fileIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}