-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketClient.java
More file actions
54 lines (47 loc) · 1.93 KB
/
Copy pathSocketClient.java
File metadata and controls
54 lines (47 loc) · 1.93 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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;
public class SocketClient extends SocketBase {
private SocketClient(final int portNum, final String serverLocation) throws IOException, IllegalArgumentException {
super(new Socket(serverLocation, portNum));
this.inputStreamReader = new InputStreamReader(this.socket.getInputStream());
this.outputStreamWriter = new OutputStreamWriter(this.socket.getOutputStream());
this.readBuffer = new BufferedReader(inputStreamReader);
this.writeBuffer = new BufferedWriter(outputStreamWriter);
}
public static void main(String[] args) {
String serverLocation = "localhost";
int portNum = 1234;
try (final SocketClient client = new SocketClient(portNum, serverLocation);) {
System.out.println("Ready to send + receive messages with server found at " + serverLocation + ":" + portNum);
client.start();
System.out.println("Client disconnecting from server... 左様なら");
} catch (IOException e) {
e.printStackTrace();
}
}
private void start() {
// Accept user input
final Scanner scanner = new Scanner(System.in);
try {
while (true) {
System.out.println("\n\n . . .");
// Works because we are based off of the same socket
System.out.println("Server Says: " + readBuffer.readLine());
final String msgToSend = scanner.nextLine();
sendMessage(msgToSend);
if (msgToSend.equalsIgnoreCase("EXIT")) {
return;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
scanner.close();
}
}
}