-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
191 lines (164 loc) · 6.39 KB
/
Client.java
File metadata and controls
191 lines (164 loc) · 6.39 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.net.*;
import java.io.*;
public class Client {
private Socket socket;
private BufferedReader br;
private PrintWriter out;
private BufferedReader console;
private File pendingFile;
private String username;
private volatile String pendingSender;
private volatile String pendingFileName;
private volatile long pendingFileSize;
public static void main(String[] args) {
new Client().start();
}
public void start() {
try {
socket = new Socket("127.0.0.1", Server.CHAT_PORT);
//I/O stream created
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
console = new BufferedReader(new InputStreamReader(System.in));
//Listener thread
new Thread(this::listen).start();
//Username handshake
if (br.readLine().equals("ENTER_USERNAME")) {
System.out.print("Enter username: ");
username = console.readLine();
out.println(username);
}
//Sending messages
while (true) {
String input = console.readLine();
if (input.equalsIgnoreCase("/exit")) {
out.println("LOGOUT");
break;
}
if(input.equalsIgnoreCase("/users")){ //get online users' list
out.println("GET_USERS");
continue;
}
if (input.startsWith("/sendfile")) {
String[] p = input.split(" ", 3);
if(p.length != 3)
out.println("ERROR");
else{
pendingFile = new File(p[2]);
out.println("FILE_REQUEST|" + p[1] + "|" + pendingFile.getName() + "|" + pendingFile.length());
//sendFile(f); //removed due to Protocol-Enforced Pairing
}
continue;
}
//private: @user hello
if (input.startsWith("@")) {
int sp = input.indexOf(" ");
if(sp != -1)
out.println("MESSAGE|" + input.substring(1, sp) + "|" + input.substring(sp + 1));
else
out.println("ERROR");
}
else {
out.println("BROADCAST|" + input);
}
if (input.equalsIgnoreCase("/accept")) {
if (pendingSender != null) {
out.println("FILE_ACCEPT|" + pendingSender + "|" + pendingFileName);
System.out.println("File accepted.");
pendingSender = null;
}
else {
System.out.println("No file request pending.");
}
continue;
}
if (input.equalsIgnoreCase("/reject")) {
if (pendingSender != null) {
System.out.println("File rejected.");
pendingSender = null;
}
else {
System.out.println("No file request pending.");
}
continue;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private void listen() {
try {
String msg;
while ((msg = br.readLine()) != null) {
System.out.println(msg);
if (msg.startsWith("FILE_OFFER")) {
String[] p = msg.split("\\|");
pendingSender = p[1];
pendingFileName = p[2];
pendingFileSize = Long.parseLong(p[3]);
System.out.println(
"Incoming file from " + pendingSender +
" : " + pendingFileName +
" (" + pendingFileSize + " bytes)"
);
System.out.println("Type /accept or /reject");
}
else if (msg.startsWith("FILE_START")) {
String[] p = msg.split("\\|");
String sender = p[1];
String fileName = p[3];
long size = Long.parseLong(p[4]);
// If I am sender
if (username.equals(sender)) {
sendFile(pendingFile);
pendingFile = null;
}
// If I am receiver
else {
receiveFile(fileName, size);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendFile(File file) { //file transfer must never run on the main thread
new Thread(() -> {
try (
Socket fs = new Socket("127.0.0.1", Server.FILE_PORT);
FileInputStream fis = new FileInputStream(file); //reads the file byte by byte, does not load whole file into memory
OutputStream os = fs.getOutputStream(); //This is a pipe ot the server
) {
byte[] buf = new byte[4096];
int read; //bytes actually read
while ((read = fis.read(buf)) != -1) { //reads bytes from file into buffer
os.write(buf, 0, read); //sent the bytes just read from '0 to read'
}
}
catch (Exception e) {
e.printStackTrace();
}
}).start();
}
private void receiveFile(String name, long size) {
new Thread(() -> {
try (
Socket fs = new Socket("127.0.0.1", Server.FILE_PORT);
FileOutputStream fos = new FileOutputStream("downloads_" + name);
InputStream is = fs.getInputStream()
) {
byte[] buf = new byte[4096];
int read;
while ((read = is.read(buf)) != -1) {
fos.write(buf, 0, read);
}
}
catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}