forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseClient.java
More file actions
47 lines (32 loc) · 1.24 KB
/
ReverseClient.java
File metadata and controls
47 lines (32 loc) · 1.24 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
package com.zetcode;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
// the client must send a bye command to
// inform the server to close the connection
public class ReverseClient {
public static void main(String[] args) throws IOException {
var hostname = "localhost";
int port = 8081;
try (var socket = new Socket(hostname, port)) {
try (var writer = new PrintWriter(socket.getOutputStream(), true)) {
try (var scanner = new Scanner(System.in)) {
try (var reader = new BufferedReader(new InputStreamReader(
socket.getInputStream()))) {
String command;
do {
System.out.print("Enter command: ");
command = scanner.nextLine();
writer.println(command);
var data = reader.readLine();
System.out.println(data);
} while (!command.equals("bye"));
}
}
}
}
}
}