Repository navigation
Expand file tree
/
Copy pathMySocket_Service.java
More file actions
75 lines (67 loc) · 1.85 KB
/
Copy pathMySocket_Service.java
File metadata and controls
75 lines (67 loc) · 1.85 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
package Java.Zone.Socket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 已经测试 OK 不玩忘记 startAccept 例子说明在 class旁边Service说明
*
* @author zone
*
*/
public abstract class MySocket_Service {
private ServerSocket server;
private Socket client;
public MySocket_Service(int port) throws IOException {
// TODO Auto-generated constructor stub
server = new ServerSocket(port);
}
/**
* 开始tcp的服务
*
* @throws IOException
*/
public void startAccept() throws IOException {
while (true) {
client = server.accept();
new Thread(new ClientThread(client)).start();
}
}
/**
* 客户端处理
*
* @author Ozone
*
*/
class ClientThread implements Runnable {
private Socket client;
private BufferedOutputStream br_out;
private BufferedInputStream br_in;
public ClientThread(Socket client) throws IOException {
// TODO Auto-generated constructor stub
this.client = client;
br_out = new BufferedOutputStream(client.getOutputStream());
br_in = new BufferedInputStream(client.getInputStream());
}
public void run() {
// TODO Auto-generated method stub
blockProcess(br_in,br_out);
try {
br_in.close();
br_out.close();
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("流与客户端关闭IOException!!!");
}
}
}
/**
* 在这里写 读写逻辑就行了
* @param br_in Client(客户端)提供给你的输入流 已经包装好了 BufferedInputStream
* @param br_out service(服务器)提供给你的输出流 已经包装好了 BufferedOutputStream
*/
public abstract void blockProcess(BufferedInputStream br_in, BufferedOutputStream br_ou) ;
}