@Override public void completed(Integer i, ByteBuffer buf) { if (i > 0) { buf.flip(); try { msg = decoder.decode(buf).toString(); System.out.println("收到" + socket.getRemoteAddress().toString() + "的消息:" + msg); buf.compact(); } catch (CharacterCodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } socket.read(buf, buf, this); try { write(socket); } catch (UnsupportedEncodingException ex) { Logger.getLogger(AioReadHandler.class.getName()).log(Level.SEVERE, null, ex); } } else if (i == -1) { try { System.out.println("客户端断线:" + socket.getRemoteAddress().toString()); buf = null; } catch (IOException e) { e.printStackTrace(); } } }
@Override public void run() { try { while (true) { String message = null; try { message = context.getQueue().take(); } catch (InterruptedException e) { log.warn("Can't read queue"); } try { writeMessage(socket, message); readMessage(socket); } catch (Throwable e) { String warning = "Client disconected"; try { warning += " ::: " + socket.getRemoteAddress(); } catch (Throwable ex) { log.error("Unexpected error : " + ex.getMessage()); } log.warn(warning); break; } } } catch (Throwable ex) { log.error("Unexpected error : " + ex.getMessage()); } finally { shutdown(); } }
private String mapRemoteAddress(AsynchronousSocketChannel channel) { try { return channel.getRemoteAddress().toString(); } catch (IOException e) { return "Unknown connection"; } }
/** * close socket,remove socket from array * * @param socket */ public static void closeConnection(AsynchronousSocketChannel socket) { try { logger.info("close client socket:" + socket.getRemoteAddress().toString()); socket.close(); } catch (IOException e) { logger.error("socket close error", e); } removeClientConnection(socket); }
public FrontendConnection(AsynchronousSocketChannel channel) throws IOException { super(channel); InetSocketAddress localAddr = (InetSocketAddress) channel.getLocalAddress(); InetSocketAddress remoteAddr = (InetSocketAddress) channel.getRemoteAddress(); this.host = remoteAddr.getHostString(); this.port = localAddr.getPort(); this.localPort = remoteAddr.getPort(); this.handler = new FrontendAuthenticator(this); }
/** 显示所有active的连接 */ public static void showAllConnections() { Enumeration<AsynchronousSocketChannel> it = allClientSocket.keys(); while (it.hasMoreElements()) { AsynchronousSocketChannel so = it.nextElement(); try { logger.info(so.getRemoteAddress().toString()); } catch (IOException e) { logger.error("SocketQueue.showAllConnections IOException:", e); } } }
public GameSession(String guid, AsynchronousSocketChannel c, long time) { this.guid = guid; channel = c; try { address = c.getRemoteAddress().toString(); } catch (IOException e) { e.printStackTrace(); } generateTime = time; heartBeatTime = time; recvQueue = new ArrayList<ProtocolPackage>(); isDispose = false; }
private void accept(AsynchronousSocketChannel channel) throws IOException { LOGGER.info("======accept========" + channel.getRemoteAddress() + ";"); ServerConnection serverConnection = new ServerConnection(GID_GENERATOR.getGid(), channel); serverConnection.regist(); }