# smart-socket **Repository Path**: JasonDai/smart-socket ## Basic Information - **Project Name**: smart-socket - **Description**: 你相信500多行代码可以实现一个java aio通信框架吗,smart-socket给你的答案是:可以的!性能表现只能用“爆炸”两个字形容,因为压测已经导致作者两台Mac Pro死机了... - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: http://smartsocket.mydoc.io/ - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1229 - **Created**: 2017-11-27 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README smart-socket is a AIO client server framework which enables quick and easy development of network applications such as protocol servers and clients.It greatly simplifies and streamlines network programming such as TCP socket server(unsupport UDP). ## Features - Better throughput, lower latency - Less resource consumptions - less code amount ## Quick Start ### Maven Dependency The smart-socket jar file is available from [Maven Central](http://mvnrepository.com/artifact/org.smartboot.socket/aio-core) and can be integrated into your dependency manager of choice from there. ```xml org.smartboot.socket aio-core 1.2.0 ``` ### Demo #### Protocol ```java public class IntegerProtocol implements Protocol { private static final int INT_LENGTH = 4; @Override public Integer decode(ByteBuffer data, AioSession session, boolean eof) { if (data.remaining() < INT_LENGTH) return null; return data.getInt(); } @Override public ByteBuffer encode(Integer s, AioSession session) { ByteBuffer b = ByteBuffer.allocate(INT_LENGTH); b.putInt(s); b.flip(); return b; } } ``` #### Writing a server ```java public class IntegerServerProcessor implements MessageProcessor { @Override public void process(AioSession session, Integer msg) { Integer respMsg = msg + 1; System.out.println("receive data from client: " + msg + " ,rsp:" + (respMsg)); try { session.write(respMsg); } catch (IOException e) { e.printStackTrace(); } } @Override public void stateEvent(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) { } } ``` ```java public class IntegerServer { public static void main(String[] args) { AioQuickServer server = new AioQuickServer() .bind(8888) .setProtocol(new IntegerProtocol()) .setProcessor(new IntegerServerProcessor()); try { server.start(); } catch (IOException e) { e.printStackTrace(); } } } ``` #### Writing a client In our case all we want to do is print the value out the the console. ```java public class IntegerClientProcessor implements MessageProcessor { private AioSession session; @Override public void process(AioSession session, Integer msg) { System.out.println("receive data from server:" + msg); } @Override public void stateEvent(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) { switch (stateMachineEnum) { case NEW_SESSION: this.session = session; break; default: System.out.println("other state:" + stateMachineEnum); } } public AioSession getSession() { return session; } } ``` ```java public class IntegerClient { public static void main(String[] args) throws Exception { IntegerClientProcessor processor = new IntegerClientProcessor(); AioQuickClient aioQuickClient = new AioQuickClient() .connect("localhost", 8888) .setProtocol(new IntegerProtocol()) .setProcessor(processor); aioQuickClient.start(); processor.getSession().write(1); Thread.sleep(1000); aioQuickClient.shutdown(); } } ``` ## License ## About us Edit By 三刀(sandao) E-mail:zhengjunweimail@163.com Update Date: 2017-11-20