-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketClientDemo2.java
More file actions
78 lines (68 loc) · 1.65 KB
/
Copy pathSocketClientDemo2.java
File metadata and controls
78 lines (68 loc) · 1.65 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.java.study;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class SocketClientDemo2 extends Socket {
private static final String SERVER_IP = "127.0.0.1";
private static final int SERVER_PORT = 2015;
private Socket client;
private BufferedReader in;
private PrintWriter out;
public SocketClientDemo2() throws Exception {
super(SERVER_IP, SERVER_PORT);
client = this;
in = new BufferedReader(new InputStreamReader(this.getInputStream()));
out = new PrintWriter(this.getOutputStream(), true);
new readLineThread();
while (true) {
/*
* stdin ==> socket
*/
in = new BufferedReader(new InputStreamReader(System.in));
String line = in.readLine();
out.println(line);
}
}
class readLineThread extends Thread {
private BufferedReader buff;
public readLineThread() {
try {
this.buff = new BufferedReader(new InputStreamReader(
client.getInputStream()));
System.out.println(buff);
start();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true) {
String line = this.buff.readLine();
if ("byeClient".equals(line)) {
break;
} else {
System.out.println(line);
}
}
in.close();
out.close();
client.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("exit client read line thread.");
}
}
}
public static void main(String[] args) {
try {
new SocketClientDemo2();// Æô¶¯¿Í»§¶Ë
} catch (Exception e) {
e.printStackTrace();
}
}
}