-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
46 lines (29 loc) · 1.06 KB
/
Copy pathClient.java
File metadata and controls
46 lines (29 loc) · 1.06 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
// Create client socket
Socket clientSocket = new Socket("127.0.0.1", 8000);
// === Send data to Server === //
// Send message to server
OutputStreamWriter out = new OutputStreamWriter(clientSocket.getOutputStream());
out.write("Hello, I am client\n");
out.flush();
// === Read data from server === //
// Read data from server
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String message = in.readLine();
// Display result
System.out.println(message);
// === Close All Connections === //
// Close OutputStreamWriter
out.close();
// Close BufferedReader
in.close();
// Close client socket
clientSocket.close();
}
}