forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketClient.java
More file actions
37 lines (34 loc) · 1.24 KB
/
SocketClient.java
File metadata and controls
37 lines (34 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
package com.examplehub.basics.network;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class SocketClient {
public static void main(String[] args) throws IOException {
Socket clientSocket = new Socket("127.0.0.1", 6666);
try (InputStream inputStream = clientSocket.getInputStream();
OutputStream outputStream = clientSocket.getOutputStream()) {
handle(inputStream, outputStream);
}
clientSocket.close();
System.out.println("disconnected.");
}
private static void handle(InputStream inputStream, OutputStream outputStream)
throws IOException {
var writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
var reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
Scanner scanner = new Scanner(System.in);
System.out.println("[server] " + reader.readLine());
for (; ; ) {
System.out.print(">>> ");
String s = scanner.nextLine();
writer.write(s + System.lineSeparator());
writer.flush();
String resp = reader.readLine();
System.out.println("[server]: " + resp);
if (resp.equals("bye")) {
break;
}
}
}
}