-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
124 lines (105 loc) · 4.11 KB
/
Client.java
File metadata and controls
124 lines (105 loc) · 4.11 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
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client {
boolean receive=true;
DataOutputStream writeData;
DataInputStream readData;
Client(Socket s) throws Exception{
readData=new DataInputStream(s.getInputStream());
writeData = new DataOutputStream(s.getOutputStream());
}
void listenClient(){
new Thread(new Runnable() {
@Override
public void run() {
while(true){
try {
char flag = 'n';
if (receive) {
while (flag == 'n') {
flag = readData.readChar();
}
receive = false;
}
if (flag == 's') {
StringBuilder filePath = new StringBuilder();
StringBuilder fileName = new StringBuilder();
int pathSize = readData.readInt();
for (int j = 0; j < pathSize; j++) {
filePath.append(readData.readChar());
}
int nameSize = readData.readInt();
for (int j = 0; j < nameSize; j++) {
fileName.append(readData.readChar());
}
receiveContents(filePath, fileName);
}
} catch (Exception e) {
System.out.println("File handling problem!");
}
}
}
}).start();
}
void sendFile(Socket s, String fileLocation) {
try {
File file = new File(fileLocation);
FileInputStream fileRead = new FileInputStream(file);
writeData.writeChar('s');
writeData.flush();
// parse the file name and type
String[] locationParts = fileLocation.split("\\\\");
String fileName = locationParts[locationParts.length - 1];
writeData.writeInt(fileName.length());
writeData.writeChars(fileName);
writeData.writeLong(file.length());
byte[] buffer = new byte[4 * 1024];
int bytes = 0;
while ((bytes = fileRead.read(buffer)) != -1) {
writeData.write(buffer, 0, bytes);
writeData.flush();
}
fileRead.close();
} catch (Exception e) {
e.printStackTrace();
}
}
void receiveContents(StringBuilder filePath, StringBuilder fileName) {
try {
File folder = new File(filePath.toString());
if (!folder.exists()) {
folder.mkdir();
}
FileOutputStream writeFile = new FileOutputStream(filePath.toString() + fileName.toString());
long fileSize = readData.readLong();
int bytes = 0;
byte[] buffer = new byte[4 * 1024];
while (fileSize>0 && (bytes = readData.read(buffer, 0, (int) Math.min(buffer.length, fileSize))) != -1) {
writeFile.write(buffer, 0, bytes);
writeFile.flush();
fileSize -= bytes;
}
receive = true;
writeFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the IP Address: ");
String ip=sc.nextLine();
System.out.println("Enter the files to send: ");
Socket s = new Socket(ip, 5001);
Client c = new Client(s);
c.listenClient();
while (s.isConnected()) {
String location = sc.nextLine();
c.sendFile(s, location);
}
s.close();
c.writeData.close();
c.readData.close();
}
}