-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileClient.java
53 lines (39 loc) · 1.43 KB
/
FileClient.java
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
import java.io.*;
import java.net.*;
public class FileClient {
public static void main(String[] args) {
// Create new socket:
Socket clientSocket = null;
// Create new PrintWriter:
PrintWriter out = null;
// Create new BufferedReader:
BufferedReader in = null;
try {
// Open new socket:
clientSocket = new Socket("127.0.0.1", 3000);
// Pass user's input to server:
out = new PrintWriter(clientSocket.getOutputStream(), true);
System.out.println("\nWelcome to the client! Enter your text below:");
} catch (Exception e) {
System.err.println(e);
}
// Get user's input:
BufferedReader stdln = new BufferedReader(new InputStreamReader(System.in));
String userInput;
try {
// Read user's input:
while((userInput = stdln.readLine()) != null) {
// Close application and sockets if 'exit' is entered:
if (userInput.equals("exit")) {
clientSocket.close();
stdln.close();
System.exit(1);
}
// Update user's input to server:
out.println(userInput);
}
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
}