Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.sheldon.javaPrj.netty; 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.stream.ChunkedWriteHandler; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author xiangnan.wang@ipinyou.com */ public class FileClient { private int port; public FileClient(int port) { this.port = port; } public void start() { EventLoopGroup workerGroup = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ChunkedWriteHandler(), new FileClientHandler()); } }); ChannelFuture f = b.connect("localhost", port).sync(); f.channel().closeFuture().sync(); } catch (InterruptedException ex) { Logger.getLogger(FileClient.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); } } public static void main(String args[]) { FileClient client = new FileClient(8888); client.start(); } }