-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.java
More file actions
85 lines (73 loc) · 2.68 KB
/
Copy pathclient.java
File metadata and controls
85 lines (73 loc) · 2.68 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
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class client{
public static String IP = "127.0.0.1";
public static int port = 5555;
public static String path = "./sample/client/";
public static void main(String[] args) {
// Get File Number
int fileCount=getFileCount(IP, port);
int count=0;
Socket socket = null;
FileOutputStream fileOutputStream = null;
// Get Files
do{
try {
socket = new Socket(IP, port);
InputStream inputStream = socket.getInputStream();
String target = path+Integer.toString(count+1)+".jpg";
fileOutputStream = new FileOutputStream(target);
byte[] dataBuff = new byte[10000];
int length = inputStream.read(dataBuff);
while (length != -1) {
fileOutputStream.write(dataBuff, 0, length);
length = inputStream.read(dataBuff);
}
System.out.println("File \"" + target +"\" is saved!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
count+=1;
}while(count<fileCount);
System.out.println("\nDisconnected!");
}
public static int getFileCount(String givneIP, int givenPort){
int getFileCount = 0;
// Receive File Count
try{
Socket initSocket = new Socket(IP, port);
InputStream isInit = initSocket.getInputStream();
DataInputStream disInit = new DataInputStream(isInit);
getFileCount = disInit.readInt();
System.out.println("Received File Count: " + getFileCount +"\n");
disInit.close();
isInit.close();
initSocket.close();
}catch(Exception e){
e.printStackTrace();
}
return getFileCount;
}
}