司马懿的西山居

如果觉得有帮助,就顶顶顶一个,或者关注、关注、关注一下,转载请注明出处。...

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 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();
			}
		}
	}
}

阅读更多
版权声明:转载请注明出处——http://blog.csdn.net/chy555chy/article https://blog.csdn.net/chy555chy/article/details/51560178
个人分类: Android
上一篇Java Zip 压缩、解压
下一篇Android UDP
想对作者说点什么? 我来说一句

AndroidTCP Client and Server

2017年07月25日 13.84MB 下载

安卓TCP client和TCP sever 开发示例

2015年12月11日 1.44MB 下载

没有更多推荐了,返回首页

关闭
关闭