【第22期】观点:IT 行业加班,到底有没有价值?

Android Tcp服务器端

原创 2016年06月02日 00:02:40
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 举报

相关文章推荐

Android实现基于TCP和UDP协议的即时通讯,含android端和服务器端

这几天学习了下在android中实现即时通讯的方法,一开始,自然是从基本的网络协议中开始尝试了,这样能够最大化的私人订制自己的应用,还能学习到更多的知识,好处多多,接下来就简单介绍下两种协议的不同点吧...

Android与服务器端通信方式(一)之HTTP、TCP、Socket

Android作为客户端,与服务器的通信方式主要有两种:一种是HTTP通信,一种是Socket通信。   一、HTTP、Socket简介 HTTP通信:即使用HTTP协议进行通信,工作原理是客户端向服...

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

黑马程序员——8.网络编程(网络模型、网络地址、传输协议(UDP、TCP/IP)、客户端和服务器端、URL)

Java网络编程(网络模型、网络地址、传输协议(UDP、TCP/IP)、客户端和服务器端、URL)

C++服务器端+Android客户端的网络通信

http://blog.sina.com.cn/s/blog_4e928b170100yg9c.html 今天晚上11点半,在学校断网后的十分钟,我终于实现了C++服务器端与Andr...

【Android Socket专题】: TCP通信服务器端app的demo的实现

UDP Client客户端  http://blog.csdn.net/shankezh/article/details/50731287 UDP Server服务器 http://blog.csd...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)