-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcpDispatcher.java
169 lines (139 loc) · 4.11 KB
/
TcpDispatcher.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package fileTransfer;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TcpDispatcher {
private String dispatchIp = "";
private int dispatchPort = 0;
private int receivePort = 0;
private ServerSocket server = null;
private ExecutorService threadPool = null;
public TcpDispatcher(String dispatchIp, int dispatchPort, int receivePort){
threadPool = Executors.newFixedThreadPool(20);
this.dispatchIp = dispatchIp;
this.dispatchPort = dispatchPort;
this.receivePort = receivePort;
}
public void execute(){
try {
server = new ServerSocket(receivePort);
Socket socket = null;
System.out.println("dispatcher started!");
while(true){
socket = server.accept();
threadPool.execute(new MessageHandler(socket));
Thread.sleep(100);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class MessageHandler implements Runnable{
private Socket socket = null;
private Socket dispatchSocket = null;
private DataInputStream rcvIn = null;
private DataOutputStream rtnOut = null;
private DataInputStream dispatchIn = null;
private DataOutputStream dispatchOut = null;
public MessageHandler(Socket socket){
this.socket = socket;
try {
dispatchSocket = new Socket(dispatchIp, dispatchPort);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
rcvIn = getRcvInputStream(socket.getInputStream());
rtnOut = getRtnOutputStream(socket.getOutputStream());
dispatchIn = getDispatchInputStream(dispatchSocket.getInputStream());
dispatchOut = getDispatchOutputStream(dispatchSocket.getOutputStream());
System.out.println("开始接受信息...");
getRcvBytes();
System.out.println("信息转发完成!");
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(rcvIn != null){rcvIn.close();}
if(rtnOut != null){rtnOut.close();}
if(dispatchIn != null){dispatchIn.close();}
if(dispatchOut != null){dispatchOut.close();}
if(socket != null){socket.close();}
if(dispatchSocket != null){dispatchSocket.close();}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private DataInputStream getRcvInputStream(InputStream in){
DataInputStream rcvIn = new DataInputStream(in);
return rcvIn;
}
private DataOutputStream getRtnOutputStream(OutputStream out){
DataOutputStream rtnOut = new DataOutputStream(out);
return rtnOut;
}
private DataInputStream getDispatchInputStream(InputStream in){
DataInputStream rcvIn = new DataInputStream(in);
return rcvIn;
}
private DataOutputStream getDispatchOutputStream(OutputStream out){
DataOutputStream rtnOut = new DataOutputStream(out);
return rtnOut;
}
private void getRcvBytes(){
byte [] buff = new byte[1024];
int length = 0;
try{
while((length = rcvIn.read(buff)) != -1){
dispatchOut.write(buff, 0, length);
dispatchOut.flush();
if(length < 1024){
getRtnBytes();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
private void getRtnBytes(){
byte [] buff = new byte[1024];
int length = 0;
try{
while((length = dispatchIn.read(buff)) != -1){
rtnOut.write(buff, 0, length);
rtnOut.flush();
if(length < 1024){
break;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String []args){
TcpDispatcher dispatcher = new TcpDispatcher("198.198.198.73", 8268, 10004);
dispatcher.execute();
}
}