使用缓冲池方式,进行TCP通信
来源:互联网 发布:php autoload怎么用 编辑:程序博客网 时间:2024/05/22 12:21
1、我贴出的主要是核心代码,有些类为自己定义,并未贴出,大家可以主要看实现思路
2、核心代码
public class Server {private ServerSocket serverSocket;private ExecutorService executorService;// 线程池private boolean isStarted = true;private Socket socket;public Server() {try {serverSocket = new ServerSocket(Constants.SERVER_PORT);executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 50);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();quit();}}public void start(){System.out.println(MyDate.getDateCN());while(isStarted){try {socket = serverSocket.accept();String ip = socket.getInetAddress().toString();System.out.println(MyDate.getDateCN() + " 用户:" + ip + " 已建立连接");if(socket.isConnected()){executorService.execute(new SocketToask(socket));}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(socket != null){try { socket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(serverSocket != null){try {serverSocket.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}}private final class SocketToask implements Runnable{private Socket socket;private InputThread in;private OutputThread out;private OutputThreadMap map;public SocketToask(Socket socket) {this.socket = socket;}@Overridepublic void run() {out = new OutputThread(socket, map);//// 先实例化写消息线程,(把对应用户的写线程存入map缓存器中)in = new InputThread(socket, out, map);// 再实例化读消息线程out.setStart(true);in.setStart(true);in.start();out.start();}}public void quit() {try {this.isStarted = false;serverSocket.close();} catch (IOException e) {e.printStackTrace();}} public static void main(String[] args) {Server server = new Server();server.start();}}
- 使用缓冲池方式,进行TCP通信
- 使用TCP/IP与服务器进行通信
- Silverlight4中用net.tcp双工方式进行通信
- java34.HTTP通信------使用HttpClient进行Get方式通信
- java35.HTTP通信------使用HttpClient进行Post方式通信
- TCP通信方式小结
- Symbian中使用TCP/IP协议进行通信
- Java TCP使用Socket进行网络通信(3)
- 使用TCP/IP的套接字(Socket)进行通信
- 使用tcp socket进行跨进程/网络通信
- 使用TCP/IP的套接字(Socket)进行通信
- 使用TCP/IP的套接字(Socket)进行通信
- 使用TCP/IP的套接字(Socket)进行通信
- 使用TCP/IP的套接字(Socket)进行通信
- Android网络编程之使用HttpClient进行Get方式通信
- Android程序:使用HttpClient进行Get方式通信
- HttpClient-----3、使用HttpClient进行GET方式通信
- HttpClient-----4、使用HttpClient进行POST方式通信
- 可视化HTML编辑器:xhEditor、FCKeditor
- 总有一天你将破蛹而出
- log4j.xml参考
- Prim算法实现最小生成树(图模型+小根堆)
- 关于CSRF攻击及mvc中的解决方案 [ValidateAntiForgeryToken]
- 使用缓冲池方式,进行TCP通信
- java多线程相关
- 设计模式之状态模式
- 用 UNetbootin 从 ISO 文件安装双系统
- POJ 1590解题报告
- CoreText总结
- 怎么控制安卓应用的权限
- AVPlayer 多媒体播放器
- nginx rewrite 参数和例子