设为首页 加入收藏

TOP

Java AIO 服务器与客户端实现示例(五)
2015-02-02 14:44:13 来源: 作者: 【 】 浏览:82
Tags:Java AIO 服务器 客户端 实现 示例
?
? ? @Override
? ? public void run() {
? ? ? ? System.out.println(Thread.currentThread().getName() + "---run");
? ? ? ?
? ? ? ? //连接服务器
? ? ? ? channel.connect(new InetSocketAddress("localhost", 8383), null, new CompletionHandler(){
? ? ? ? ? ? final ByteBuffer readBuffer = ByteBuffer.allocateDirect(1024);
? ? ? ? ? ?
? ? ? ? ? ? @Override
? ? ? ? ? ? public void completed(Void result, Void attachment) {
? ? ? ? ? ? ? ? //连接成功后, 异步调用OS向服务器写一条消息
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? //channel.write(CharsetHelper.encode(CharBuffer.wrap(helper.getWord())));
? ? ? ? ? ? ? ? ? ? writeStringMessage(helper.getWord());
? ? ? ? ? ? ? ? } catch (CharacterCodingException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? //helper.sleep();//等待写异步调用完成
? ? ? ? ? ? ? ? readBuffer.clear();
? ? ? ? ? ? ? ? //异步调用OS读取服务器发送的消息
? ? ? ? ? ? ? ? channel.read(readBuffer, null, new CompletionHandler(){
?
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void completed(Integer result, Object attachment) {
? ? ? ? ? ? ? ? ? ? ? ? try{
? ? ? ? ? ? ? ? ? ? ? ? ? ? //异步读取完成后处理
? ? ? ? ? ? ? ? ? ? ? ? ? ? if(result > 0){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? readBuffer.flip();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CharBuffer charBuffer = CharsetHelper.decode(readBuffer);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String answer = charBuffer.toString();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println(Thread.currentThread().getName() + "---" + answer);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? readBuffer.clear();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String word = helper.getWord();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(word != null){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //异步写
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //channel.write(CharsetHelper.encode(CharBuffer.wrap(word)));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? writeStringMessage(word);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //helper.sleep();//等待异步操作
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? channel.read(readBuffer, null, this);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //不想发消息了,主动关闭channel
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? shutdown();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //对方已经关闭channel,自己被动关闭,避免空循环
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? shutdown();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? catch(Exception e){
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ?
?
? ? ? ? ? ? ? ? ? ? /**
? ? ? ? ? ? ? ? ? ? * 读取失败处理
? ? ? ? ? ? ? ? ? ? * @param exc
? ? ? ? ? ? ? ? ? ? * @param attachment
? ? ? ? ? ? ? ? ? ? */
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void failed(Throwable exc, Object attachment) {
? ? ? ? ? ? ? ? ? ? ? ? System.out.println("client read failed: " + exc);
? ? ? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? ? ? shutdown();
? ? ? ? ? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? });
? ? ? ? ? ? }
?
? ? ? ? ? ? /**
? ? ? ? ? ? * 连接失败处理
? ? ? ? ? ? * @param exc
? ? ? ? ? ? * @param attachment
? ? ? ? ? ? */
? ? ? ? ? ? @Override
? ? ? ? ? ? public void failed(Throwable exc, Void attachment) {
? ? ? ? ? ? ? ? System.out.println("client connect to server failed: " + exc);
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? shutdown();
? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }? ? ? ? ?
? ? ? ? });? ?
? ? }
? ?
? ? private void shutdown() throws IOException {
? ? ? ? if(channel != null){
? ? ? ? ? ? channel.close();
? ? ? ? }
? ? ? ?
? ? ? ? latch.countDown();? ? ? ? ? ? ? ? ? ? ? ? ?
? ? }
? ?
? ? /**
? ? * Enqueues a write of the buffer to the channel.
? ? * The call is asynchronous so the buffer is not safe to modify after
? ? * passing the buffer here.
? ? *
? ? * @param buffer the buffer to send to the channel
? ? */
? ? private void writeMessage(final ByteBuffer buffer) {
? ? ? ? boolean threadShouldWrite = fal
首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java NIO操作类型 下一篇Java NIO 服务器与客户端实现示例

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: