package com.ewing.netty.demo.client;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ewing.netty.demo.service.XLServer;
/**
* 服务器特征:
* 1、使用专用解码器解析服务器发过来的数据
* 2、客户端主动关闭连接
*/
public class XLClient {
public static final int port = XLServer.port;
public static final String host = "localhost";
private static final Logger logger = LoggerFactory.getLogger(XLClient.class);
private static final NioClientSocketChannelFactory clientSocketChannelFactory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
private static final ClientBootstrap clientBootstrap = new ClientBootstrap(clientSocketChannelFactory);
/**
* 启动客户端
* @return
* @throws Exception
*/
public static ChannelFuture startup() throws Exception {
/**
* 注意:由于XLClientHandler中有状态的成员变量,因此不能采用默认共享ChannelPipeline的方式
* 例如,下面的代码形式是错误的:
* ChannelPipeline pipeline=clientBootstrap.getPipeline();
* pipeline.addLast("handler", new XLClientHandler());
*/
clientBootstrap.setPipelineFactory(new XLClientPipelineFactory()); // 只能这样设置
/**
* 请注意,这里不存在使用“child.”前缀的配置项,客户端的SocketChannel实例不存在父级Channel对象
*/
clientBootstrap.setOption("tcpNoDelay", true);
clientBootstrap.setOption("keepAlive", true);
ChannelFuture future = clientBootstrap.connect(new InetSocketAddress(host, port));
/**
* 阻塞式的等待,直到ChannelFuture对象返回这个连接操作的成功或失败状态
*/
future.awaitUninterruptibly();
/**
* 如果连接失败,我们将打印连接失败的原因。
* 如果连接操作没有成功或者被取消,ChannelFuture对象的getCause()方法将返回连接失败的原因。
*/
if (!future.isSuccess()) {
future.getCause().printStackTrace();
} else {
logger.info("client is connected to server " + host + ":" + port);
}
return future;
}
/**
* 关闭客户端
* @param future
* @throws Exception
*/
public static void shutdown(ChannelFuture future) throws Exception {
try {
/**
* 主动关闭客户端连接,会阻塞等待直到通道关闭
*/
future.getChannel().close().awaitUninterruptibly();
// future.getChannel().getCloseFuture().awaitUninterruptibly();
/**
* 释放ChannelFactory通道工厂使用的资源。
* 这一步仅需要调用 releaseExternalResources()方法即可。
* 包括NIO Secector和线程池在内的所有资源将被自动的关闭和终止。
*/
clientBootstrap.releaseExternalResources();
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
} finally {
System.exit(1);
logger.info("client is shutdown to server " + host + ":" + port);
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ChannelFuture future = XLClient.startup();
logger.info("future state is " + future.isSuccess());
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
netty实例.zip (36个子文件)
netty_binary
.project 1014B
.settings
org.eclipse.core.resources.prefs 159B
org.eclipse.jdt.core.prefs 330B
.mymetadata 309B
WebRoot
WEB-INF
web.xml 381B
lib
netty-3.6.6.Final.jar 1.15MB
slf4j-log4j12-1.7.5.jar 9KB
log4j-1.2.17.jar 479KB
slf4j-api-1.7.5.jar 25KB
classes
log4j.properties 1KB
com
ewing
netty
demo
vo
XLRequest.class 3KB
XLResponse.class 3KB
codec
XLClientDecoder.class 2KB
XLServerEncoder.class 3KB
client
XLClientHandler.class 3KB
XLCharSetFactory.class 1KB
XLClient.class 3KB
XLClientPipelineFactory.class 991B
service
XLServerHandler.class 4KB
XLServer.class 4KB
util
ProtocolUtil.class 4KB
index.jsp 834B
META-INF
MANIFEST.MF 39B
.myeclipse
src
log4j.properties 1KB
com
ewing
netty
demo
vo
XLResponse.java 3KB
XLRequest.java 3KB
codec
XLServerEncoder.java 2KB
XLClientDecoder.java 2KB
client
XLClientPipelineFactory.java 961B
XLClient.java 4KB
XLCharSetFactory.java 1KB
XLClientHandler.java 2KB
service
XLServer.java 3KB
XLServerHandler.java 3KB
util
ProtocolUtil.java 4KB
.classpath 677B
共 36 条
- 1
libinbingo
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页