-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClient.java
More file actions
43 lines (36 loc) · 1.24 KB
/
Client.java
File metadata and controls
43 lines (36 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
package lesson6;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 80);
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
Scanner input = new Scanner(System.in);
Thread receiveServerMessageThread = new Thread(() -> {
while (true) {
try {
String serverMessage = in.readUTF();
System.out.println("Message from server: " + serverMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
});
receiveServerMessageThread.start();
while (true) {
String message = input.nextLine();
if (message.equals("quit")) {
System.out.println("Finished!");
out.writeUTF(message);
out.flush();
break;
}
out.writeUTF(message);
out.flush();
}
}
}