-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
60 lines (46 loc) · 1.67 KB
/
Client.java
File metadata and controls
60 lines (46 loc) · 1.67 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
import java.io.*;
import java.net.*;
import java.util.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class Client {
//Information needed to get a connection with the server
private static final String HOST = "localhost";
private static final int PORT = 9999;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//Connecting to the server
try {
Socket socket = new Socket(HOST, PORT);
System.out.println("Connected");
OutputStream output = socket.getOutputStream();
InputStream input = socket.getInputStream();
PrintWriter out = new PrintWriter(output);
Scanner in = new Scanner(input);
//Get user input
while (true) {
System.out.println("Enter your command (or \"exit\" to quit):");
String stringOutput = scanner.nextLine();
while (stringOutput.equals("")) {
System.out.println("Enter again:");
stringOutput = scanner.nextLine();
}
if (stringOutput.equals("exit")) {
break;
}
out.println(stringOutput);
out.flush();
while (in.hasNextLine()) {
String line = in.nextLine();
if (line.length() == 0) {
break;
}
System.out.println(line);
}
}
socket.close();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}