-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocketTCP.java
219 lines (208 loc) · 7.68 KB
/
SocketTCP.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.mellow.net;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import com.mellow.data.QueryData;
import com.mellow.interfas.SocketRequest;
import com.mellow.tool.LogSwitch;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy.Builder;
public class SocketTCP {
private final String TAG;
private final Context context;
// 变量
private String address;
private Socket socket;
public boolean isConnected;
private InputStream inputStream;
private OutputStream outputStream;
private ExecutorService cachedThreadPool;
// 回调
private SocketRequest socketRequest;
// 常量
public final int Conn_CreateSucs = 0x00;
public final int Conn_TimeOut = 0x01;
public final int Conn_CreateFail = 0x02;
public final int Conn_ReceiveDone = 0x03;
public final int Conn_ServerClosed = 0x04;
public final int Conn_LocalClosed = 0x05;
public final int Error_NumberFormat = 0xFF01;
public final int Error_UnknownHost = 0xFF02;
public final int Error_IO = 0xFF03;
public final int Error_NullPointer = 0xFF04;
/**
* @param context 传入上下文
*/
public SocketTCP(Context context) {
this.TAG = getClass().getSimpleName();
this.context = context;
this.cachedThreadPool = Executors.newCachedThreadPool();
if (android.os.Build.VERSION.SDK_INT > 8) {
Builder builder = new StrictMode.ThreadPolicy.Builder();
builder.detectDiskReads();
builder.detectDiskWrites();
builder.detectNetwork();
StrictMode.setThreadPolicy(builder.build());
android.os.StrictMode.VmPolicy.Builder VMBuilder = new StrictMode.VmPolicy.Builder();
VMBuilder.detectLeakedSqlLiteObjects();
VMBuilder.penaltyDeath();
StrictMode.setVmPolicy(VMBuilder.build());
}
}
/**
* @param address 服务器地址和端口
* @param socketRequest 回调接口
*/
public void initConnection(String address, SocketRequest socketRequest) {
this.address = address;
this.socketRequest = socketRequest;
if (socket != null) {
// 关闭连接
closeConnection();
}
// 线程池里创建连接
cachedThreadPool.execute(runnableSocket);
int amount = ((ThreadPoolExecutor) cachedThreadPool).getActiveCount();
LogSwitch.d(TAG, "initConnection", "Now the thread amount = " + amount);
}
// 创建Socket连接
private Runnable runnableSocket = new Runnable() {
@Override
public void run() {
String[] ads = address.split(":");
boolean result = false;
try {
String[] ports = new QueryData(context).getPortInfo(null);
boolean hasLocal = ports != null && ports[0] != null;
if (hasLocal) {
String port = ports[0].replaceAll("[^0-9]", "");
if (port.length() > 0 && !port.equals("0")) {
int localPort = Integer.parseInt(port);
socket = new Socket();
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(localPort));
socket.connect(new InetSocketAddress(ads[0], Integer.parseInt(ads[1])));
}
} else {
socket = new Socket();
socket.connect(new InetSocketAddress(ads[0], Integer.parseInt(ads[1])));
}
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
handler.sendEmptyMessage(Conn_CreateSucs);
isConnected = true;
result = true;
byte[] buffer = new byte[8192];
byte[] buff = null;
while (inputStream != null) {
int length = inputStream.read(buffer);
if (length <= 0) {
// 服务器已断开
handler.sendEmptyMessage(Conn_ServerClosed);
// 置空数据
buffer = null;
buff = null;
// 关闭连接
closeConnection();
return;
} else {
buff = new byte[length];
System.arraycopy(buffer, 0, buff, 0, length);
Message msg = new Message();
msg.what = Conn_ReceiveDone;
msg.obj = buff;
handler.sendMessage(msg);
}
}
} catch (NumberFormatException e) {
LogSwitch.e(TAG, "initConnection", "NumberFormatException", e);
} catch (UnknownHostException e) {
LogSwitch.e(TAG, "initConnection", "UnknownHostException", e);
} catch (IOException e) {
LogSwitch.e(TAG, "initConnection", "IOException", e);
} catch (NullPointerException e) {
LogSwitch.e(TAG, "initConnection", "NullPointerException", e);
} catch (IllegalArgumentException e) {
LogSwitch.e(TAG, "initConnection", "IllegalArgumentException", e);
}
if (!result) {
handler.sendEmptyMessage(Conn_CreateFail);
} else {
handler.sendEmptyMessage(Conn_ServerClosed);
}
isConnected = false;
}
};
/**
* @param msg 需要发送的自己数组
* @return 返回发送结果
*/
public boolean sendBytes(byte[] buffer) {
try {
outputStream.write(buffer);
outputStream.flush();
return true;
} catch (IOException e) {
LogSwitch.e(TAG, "sendBytes", "IOException", e);
} catch (NullPointerException e) {
LogSwitch.e(TAG, "sendBytes", "NullPointerException", e);
}
isConnected = false;
return false;
}
/**
* 关闭连接
*/
public void closeConnection() {
// 已从本地关闭连接
isConnected = false;
handler.sendEmptyMessage(Conn_LocalClosed);
if (socket != null) {
try {
socket.close();
socket = null;
} catch (IOException e) {
LogSwitch.e(TAG, "closeConnection", "socket", e);
}
}
if (inputStream != null) {
try {
inputStream.close();
inputStream = null;
} catch (IOException e) {
LogSwitch.e(TAG, "closeConnection", "inputStream", e);
}
}
if (outputStream != null) {
try {
outputStream.close();
outputStream = null;
} catch (IOException e) {
LogSwitch.e(TAG, "closeConnection", "outputStream", e);
}
}
// 置空回调接口
socketRequest = null;
}
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (socketRequest != null) {
socketRequest.result(msg.what, (byte[]) msg.obj);
}
super.handleMessage(msg);
}
};
}