Android Tcp服务器端
【摘要】
package com.chy.socket; import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import jav...
-
package com.chy.socket;
-
-
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.io.OutputStream;
-
import java.net.ServerSocket;
-
import java.net.Socket;
-
import java.util.ArrayList;
-
import java.util.List;
-
import java.util.concurrent.ExecutorService;
-
import java.util.concurrent.Executors;
-
-
-
import com.chy.global.GlobalValue;
-
-
-
public class TcpServer {
-
-
-
private ServerSocket serverSocket = null;
-
private List<Socket> clientList = null;
-
private OnReceiveListener onReceiveListener = null;
-
private ExecutorService executorService = null;
-
-
-
public interface OnReceiveListener {
-
public void onReceive(Socket socket, OutputStream os, byte[] buffer, int len);
-
}
-
-
-
class ReadRunnable implements Runnable {
-
-
-
private Socket socket = null;
-
private InputStream is = null;
-
private OutputStream os = null;
-
-
-
public ReadRunnable(Socket socket) {
-
this.socket = socket;
-
try {
-
is = socket.getInputStream();
-
os = socket.getOutputStream();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
-
-
@Override
-
public void run() {
-
int len = 0;
-
byte[] buffer = new byte[GlobalValue.READ_BLOCK_SIZE];
-
while (true) {
-
try {
-
len = is.read(buffer, 0, GlobalValue.READ_BLOCK_SIZE);
-
if (len > 0) {
-
if (onReceiveListener != null) {
-
onReceiveListener.onReceive(socket, os, buffer, len);
-
}
-
}
-
} catch (IOException ex) {
-
ex.printStackTrace();
-
break;
-
}
-
}
-
clientList.remove(socket);
-
closeClient(socket);
-
}
-
-
-
}
-
-
-
public TcpServer(int port, OnReceiveListener onReceivedListener) {
-
try {
-
serverSocket = new ServerSocket(port);
-
} catch (IOException ex) {
-
ex.printStackTrace();
-
}
-
if (serverSocket == null) {
-
return;
-
}
-
this.onReceiveListener = onReceivedListener;
-
clientList = new ArrayList<Socket>();
-
executorService = Executors.newFixedThreadPool(GlobalValue.TCP_MAX_CONNECT_COUNT);
-
// 监听线程
-
executorService.execute(new Runnable() {
-
@Override
-
public void run() {
-
Socket socket = null;
-
while (true) {
-
try {
-
socket = serverSocket.accept();
-
clientList.add(socket);
-
executorService.execute(new ReadRunnable(socket));
-
} catch (IOException ex) {
-
ex.printStackTrace();
-
break;
-
}
-
}
-
closeClient(socket);
-
}
-
});
-
}
-
-
-
public synchronized boolean send(Socket socket, OutputStream os, byte[] buffer) {
-
boolean result = false;
-
if (os != null) {
-
try {
-
os.write(buffer);
-
os.flush();
-
result = true;
-
} catch (IOException ex) {
-
ex.printStackTrace();
-
}
-
}
-
return result;
-
}
-
-
-
public void closeClient(Socket socket) {
-
if (socket == null) {
-
return;
-
}
-
try {
-
InputStream is = socket.getInputStream();
-
if (is != null) {
-
is.close();
-
}
-
OutputStream os = socket.getOutputStream();
-
if (os != null) {
-
os.close();
-
}
-
socket.close();
-
} catch (IOException ex) {
-
ex.printStackTrace();
-
}
-
}
-
-
-
public void close() {
-
if (clientList != null) {
-
for (Socket socket : clientList) {
-
closeClient(socket);
-
}
-
}
-
if (serverSocket != null) {
-
try {
-
serverSocket.close();
-
} catch (IOException ex) {
-
ex.printStackTrace();
-
}
-
}
-
}
-
}
文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/chy555chy/article/details/51560178
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)