-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.java
More file actions
111 lines (97 loc) · 3.45 KB
/
Copy pathserver.java
File metadata and controls
111 lines (97 loc) · 3.45 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
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
public static int port = 5555;
public static ServerSocket serverSocket = null;
public static String path = "./sample/server/";
public static void main(String[] args) {
try {
serverSocket = new ServerSocket(port);
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println("Wait ... ");
while(true){
File dir = new File(path);
int fileCount = folderFileCount(dir);
//Socket initSock = null;
// Send file count
sendFileCount(fileCount);
FileInputStream fileInputStream = null;
Socket socket = null;
int count = 0;
// Send File
do{
try {
socket = serverSocket.accept();
OutputStream outputStream = socket.getOutputStream();
String target = path+Integer.toString(count+1)+".jpg";
fileInputStream = new FileInputStream(target);
byte[] dataBuff = new byte[10000];
int length = fileInputStream.read(dataBuff);
while (length != -1) {
outputStream.write(dataBuff, 0, length);
length = fileInputStream.read(dataBuff);
}
System.out.println("File \""+ target + "\" is trasmitted!");
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (fileInputStream != null) {
try {
fileInputStream.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("\nDisconnect\n");
}
}
public static int folderFileCount(File givenDir){
int fileCount = 0;
try{
for(File file:givenDir.listFiles()){
if(file.isFile()){
fileCount+=1;
}
}
}catch(Exception e){
e.printStackTrace();
}
return fileCount;
}
public static void sendFileCount(int givenFileCount){
try{
Socket initSocket = serverSocket.accept();
OutputStream osInit = initSocket.getOutputStream();
DataOutputStream dosInit = new DataOutputStream(osInit);
dosInit.writeInt(givenFileCount);
System.out.println("File Number is transmitted!\n");
dosInit.close();
osInit.close();
initSocket.close();
}catch(Exception e){
e.printStackTrace();
}
}
}