-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClientApp.java
52 lines (48 loc) · 1.64 KB
/
ClientApp.java
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
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class ClientApp {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("35.200.251.56", 2000);
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
Thread read = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
if (is.available() > 0) {
int d = 0;
String msg = "";
while ((d = is.read()) != 38) {
msg = msg + (char) d;
}
System.out.println(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
read.start();
Thread write = new Thread(new Runnable() {
@Override
public void run() {
Scanner sc = new Scanner(System.in);
while (true) {
String msg = sc.nextLine();
try {
os.write((msg + "&").getBytes());
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
write.start();
}
}