-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClient.java
More file actions
53 lines (51 loc) · 1.37 KB
/
Client.java
File metadata and controls
53 lines (51 loc) · 1.37 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
Socket clientSocket;
BufferedReader in;
PrintWriter out;
Scanner keyboard = new Scanner(System.in);
try {
clientSocket = new Socket("127.0.0.1", 5000);
out = new PrintWriter(clientSocket.getOutputStream());
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
Thread sender = new Thread(new Runnable() {
String msg;
@Override
public void run() {
while (true) {
msg = keyboard.nextLine();
out.println(msg);
out.flush();
}
}
});
sender.start();
Thread receiver = new Thread(new Runnable() {
String msg;
@Override
public void run() {
try {
msg = in.readLine();
while (msg != null) {
System.out.println("Server : " + msg);
msg = in.readLine();
}
out.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
receiver.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}