我这个netty5 TimeServer为什么达不到效果?

问题描述

我这个netty5 TimeServer为什么达不到效果?

/*

  • Copyright 2013-2018 Lilinfeng.
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  • http://www.apache.org/licenses/LICENSE-2.0
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.
    */
    package com.phei.netty.basic;

import java.nio.charset.Charset;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

/**

  • @author lilinfeng
  • @date 2014年2月14日
  • @version 1.0
    */
    public class TimeServer {

    public void bind(int port) throws Exception {
    // 配置服务端的NIO线程组
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup)
    .channel(NioServerSocketChannel.class)
    .option(ChannelOption.SO_BACKLOG, 1024)
    .childHandler(new ChildChannelHandler());
    // 绑定端口,同步等待成功
    ChannelFuture f = b.bind(port).sync();

    // 等待服务端监听端口关闭
    f.channel().closeFuture().sync();
    

    } finally {
    // 优雅退出,释放线程池资源
    bossGroup.shutdownGracefully();
    workerGroup.shutdownGracefully();
    }
    }

    private class ChildChannelHandler extends ChannelInitializer {
    @Override
    protected void initChannel(SocketChannel arg0) throws Exception {
    arg0.pipeline().addLast(new LineBasedFrameDecoder(1024));
    arg0.pipeline().addLast(new StringDecoder(Charset.forName("UTF-8")));
    arg0.pipeline().addLast(new TimeServerHandler());

    }

    }

    /**

    • @param args
    • @throws Exception
      */
      public static void main(String[] args) throws Exception {
      int port = 8080;
      if (args != null && args.length > 0) {
      try {
      port = Integer.valueOf(args[0]);
      } catch (NumberFormatException e) {
      // 采用默认值
      }
      }
      new TimeServer().bind(port);
      }
      }

/*

  • Copyright 2012 The Netty Project
    *
  • The Netty Project licenses this file to you under the Apache License,
  • version 2.0 (the "License"); you may not use this file except in compliance
  • with the License. You may obtain a copy of the License at:
    *
  • http://www.apache.org/licenses/LICENSE-2.0
    *
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  • WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  • License for the specific language governing permissions and limitations
  • under the License.
    */
    package com.phei.netty.basic;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

/**

  • @author lilinfeng
  • @date 2014年2月14日
  • @version 1.0
    */
    public class TimeServerHandler extends ChannelHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
    throws Exception {
    String body = (String)msg;
    System.out.println("The time server receive order : " + body);
    String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new java.util.Date(
    System.currentTimeMillis()).toString() : "BAD ORDER";
    ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
    ctx.writeAndFlush(resp);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

    cause.printStackTrace();
    ctx.close();
    

    }
    }

/*

  • Copyright 2013-2018 Lilinfeng.
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  • http://www.apache.org/licenses/LICENSE-2.0
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.
    */
    package com.phei.netty.basic;

import java.nio.charset.Charset;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

/**

  • @author lilinfeng
  • @date 2014年2月14日
  • @version 1.0
    */
    public class TimeClient {

    public void connect(int port, String host) throws Exception {
    // 配置客户端NIO线程组
    EventLoopGroup group = new NioEventLoopGroup();
    try {
    Bootstrap b = new Bootstrap();
    b.group(group).channel(NioSocketChannel.class)
    .option(ChannelOption.TCP_NODELAY, true)
    .handler(new ChannelInitializer() {
    @Override
    public void initChannel(SocketChannel ch)
    throws Exception {
    ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
    ch.pipeline().addLast(new StringDecoder(Charset.forName("UTF-8")));
    ch.pipeline().addLast(new TimeClientHandler());
    }
    });

    // 发起异步连接操作
    ChannelFuture f = b.connect(host, port).sync();
    
    // 当代客户端链路关闭
    f.channel().closeFuture().sync();
    

    } finally {
    // 优雅退出,释放NIO线程组
    group.shutdownGracefully();
    }
    }

    /**

    • @param args
    • @throws Exception
      */
      public static void main(String[] args) throws Exception {
      int port = 8080;
      if (args != null && args.length > 0) {
      try {
      port = Integer.valueOf(args[0]);
      } catch (NumberFormatException e) {
      // 采用默认值
      }
      }
      new TimeClient().connect(port, "127.0.0.1");
      }
      }

/*

  • Copyright 2012 The Netty Project
    *
  • The Netty Project licenses this file to you under the Apache License,
  • version 2.0 (the "License"); you may not use this file except in compliance
  • with the License. You may obtain a copy of the License at:
    *
  • http://www.apache.org/licenses/LICENSE-2.0
    *
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  • WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  • License for the specific language governing permissions and limitations
  • under the License.
    */
    package com.phei.netty.basic;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.util.logging.Logger;

/**

  • @author lilinfeng
  • @date 2014年2月14日
  • @version 1.0
    */
    public class TimeClientHandler extends ChannelHandlerAdapter {
    private byte[] req;

    private static final Logger logger = Logger
    .getLogger(TimeClientHandler.class.getName());

    /**

    • Creates a client-side handler.
      */
      public TimeClientHandler() {
      req = ("QUERY TIME ORDER"+System.getProperty("line.seperator")).getBytes();
      }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
    ByteBuf message = null;
    for(int i = 0;i<100;i++){
    message = Unpooled.buffer(req.length);
    message.writeBytes(req);
    ctx.writeAndFlush(message);
    }
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
    throws Exception {
    String body = (String) msg;
    System.out.println("Now is : " + body);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    // 释放资源
    logger.warning("Unexpected exception from downstream : "
    + cause.getMessage());
    cause.printStackTrace();
    ctx.close();
    }
    }

解决方案

运行你的程序,看报什么错误。这是第一步,如果没有报错,用标准的客户端和标准的服务器端分别测试你的客户端和服务器端看能不能工作。如果在开发环境可以工作,运行环境不行,那么检查环境、配置等问题。

时间: 2024-10-06 10:47:18

我这个netty5 TimeServer为什么达不到效果?的相关文章

运用OpenGL ES 2.0实现各种各样图像滤镜(图像处理)效果,多达50多种效果。

实现各种各样图像滤镜(图像处理)效果,多达50多种效果,基本囊括了最常见的图像处理效果.包括:contrast,hue,gamma,brightness,sharpness,emboss,saturation,exposure,shadow,blend等等. 由于需要用到OpenGL ES 2.0,所以,仅能在真机中测试.可以载入本地相册的照片或者直接拍照后进行照片处理. 源码下载地址:http://download.csdn.net/detail/gulaer/6576371 转载地址:htt

大家帮我看看下面这个Struts练习哪儿错了?为什么达不到效果?

问题描述 大家帮我看看下面这个Struts练习哪儿错了?为什么达不到效果?资料说点击链接,能赚到Hello.jsp上面,可是我点击链接,提示找不到hello.aciton.   这个是struts.xml<?xml version="1.0" encoding="utf-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration

做一个城市切换,为什么达不到效果

问题描述 我做一个城市切换,首先判断如果是输入顶级域名进来就直接根据IP定位,如果不是就直接保存COOKIES,但是现在遇到问题就是COOKIES里面的值为空StringBuildercityString=newStringBuilder();stringipaddress=System.Web.HttpContext.Current.Request.UserHostAddress;stringstr=HttpContext.Current.Request.Url.Host.ToLower();

pl/sqldeveloper执行光标所在行

  一.发现问题 需要执行某一行语句时,需要选中该行语句,点击F8才行. 二.解决问题 中文版:工具->首选项->SQL窗口->自动选择语句 英文版:tools->preferences->sql window->autoSelect Statement 这样光标放到某一行,点击F8就可执行该行,不要全选该行. 三.注意点 该语句必须以分号结尾,否则达不到效果.

pl/sql developer执行光标所在行

一.发现问题需要执行某一行语句时,需要选中该行语句,点击F8才行. 二.解决问题中文版:工具->首选项->SQL窗口->自动选择语句 英文版:tools->preferences->sql window->autoSelect Statement 这样光标放到某一行,点击F8就可执行该行,不要全选该行. 三.注意点该语句必须以分号结尾,否则达不到效果.

教程/dreamweaver/提高 表格妙用-线框制作详解(3)

dreamweaver|教程|详解 细线边框的制作 细线边框是网页中定位区分内容常用的一种方法,配合特定图片的使用,往往能够达到不错的效果,那么如何制作细线边框呢? 大家都知道,所谓细线边框,大体外观上就是一个简单的1行1列表格,那么我们直接把表格对象的Border值设定为1不就可以了吗?请看下面的制作效果: 同样在网页中指定位置插入一个单行单列的表格,用鼠标单击表格外框的任何一个部位以选中它,在属性板中将"Border"值设定为1,也可以同时将另外的"CellPad&quo

DreamWeaver表格妙用-线框制作详解(3)

dreamweaver|详解 2> 细线边框的制作 细线边框是网页中定位区分内容常用的一种方法,配合特定图片的使用,往往能够达到不错的效果,那么如何制作细线边框呢? 大家都知道,所谓细线边框,大体外观上就是一个简单的1行1列表格,那么我们直接把表格对象的Border值设定为1不就可以了吗?请看下面的制作效果: 同样在网页中指定位置插入一个单行单列的表格,用鼠标单击表格外框的任何一个部位以选中它,在属性板中将"Border"值设定为1,也可以同时将另外的"CellPad&

行业网站活动推广实例分析

推广 网站运营探讨俱乐部活动推广实例背景介绍: 讨论内容:"赛维拉"杯-营商电动车网论坛优秀版主及会员评选活动推广方式 活动页面:http://www.ebb365.com/bbs/dispbbs.asp?boardID=59&ID=21162&page=1 受众人群:月收入4000以下,男女比例基本持平,老人也占较大比重. 条件限制:因为网站属性,所以奖品设置有限制.该网站个人用户基础本身比较薄弱,属于比较新的行业站,可利用的行业资源有限.   1.活动周期问题:从活

JSP+JDBC(Thin模式)连接Oracle

js|oracle 在JSP中连接到Oracle一般有2种方式: 1.Oracle JDBC的oci8方式 2.Oracle JDBC的thin方式 我比较喜欢第2种,因为WEB发布服务器与数据库服务器一般都不会放在同一台电脑中,而在使用thin方式连接时,WEB服务器端无须安装oracle的客户端. 在动手先代码之前,我们先把环境配置妥善.先从安装了Oracle的数据库服务器中,找到Oracle安装目录,然后将该目录下的jdbc\lib\classes12.jar文件拷贝到WEB发布服务器的某