-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1263919792@qq.com
committed
Jul 24, 2019
1 parent
1434fea
commit 74a959c
Showing
5 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package netty_demo.fileregion_demo; | ||
|
||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelInitializer; | ||
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 java.net.InetSocketAddress; | ||
|
||
public class EchoClient { | ||
|
||
private String host; | ||
private int port; | ||
|
||
public EchoClient(String host, int port) { | ||
this.host = host; | ||
this.port = port; | ||
} | ||
|
||
private void start() throws Exception { | ||
EventLoopGroup group = new NioEventLoopGroup(); | ||
|
||
try { | ||
Bootstrap b = new Bootstrap(); | ||
b.group(group) | ||
.channel(NioSocketChannel.class) | ||
.remoteAddress(new InetSocketAddress(host, port)) | ||
.handler(new ChannelInitializer<SocketChannel>() { | ||
protected void initChannel(SocketChannel ch) throws Exception { | ||
ch.pipeline().addLast(new EchoClientHandler()); | ||
} | ||
}); | ||
|
||
ChannelFuture channelFuture = b.connect().sync(); | ||
channelFuture.channel().closeFuture().sync(); | ||
} finally { | ||
group.shutdownGracefully(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
String host = "127.0.0.1"; | ||
int port = 8080; | ||
|
||
if (args != null && args.length > 0) { | ||
host = args[0]; | ||
port = Integer.valueOf(args[1]); | ||
} | ||
|
||
new EchoClient(host, port).start(); | ||
} | ||
|
||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/netty_demo/fileregion_demo/EchoClientHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package netty_demo.fileregion_demo; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.*; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
|
||
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> { | ||
|
||
@Override | ||
public void channelActive(ChannelHandlerContext ctx) throws Exception { | ||
File file = new File("D:\\WorkSpace\\netttyDemo\\src\\main\\java" + | ||
"\\netty_demo\\bigdata_demo\\demo.txt"); | ||
FileInputStream inputStream = new FileInputStream(file); | ||
DefaultFileRegion fileRegion = new DefaultFileRegion(inputStream.getChannel(), 0, file.length()); | ||
ctx.writeAndFlush(fileRegion).addListener(new ChannelFutureListener() { | ||
@Override | ||
public void operationComplete(ChannelFuture future) throws Exception { | ||
if (future.isSuccess()) { | ||
System.out.println("写成功"); | ||
} else { | ||
System.out.println("写失败"); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { | ||
|
||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | ||
cause.printStackTrace(); | ||
ctx.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package netty_demo.fileregion_demo; | ||
|
||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelInitializer; | ||
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 java.net.InetSocketAddress; | ||
|
||
public class EchoServer { | ||
|
||
private int port; | ||
|
||
public EchoServer(int port) { | ||
this.port = port; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
int port = 8080; | ||
if (args != null && args.length > 0) { | ||
port = Integer.valueOf(args[0]); | ||
} | ||
|
||
new EchoServer(port).start(); | ||
} | ||
|
||
private void start() throws Exception { | ||
EventLoopGroup group = new NioEventLoopGroup(); | ||
|
||
try { | ||
ServerBootstrap serverBootstrap = new ServerBootstrap(); | ||
serverBootstrap.group(group) | ||
.channel(NioServerSocketChannel.class) | ||
.localAddress(new InetSocketAddress(port)) | ||
.childHandler(new ChannelInitializer<SocketChannel>() { | ||
|
||
protected void initChannel(SocketChannel socketChannel) throws Exception { | ||
socketChannel.pipeline().addLast(new EchoServerHandler()); | ||
} | ||
}); | ||
|
||
ChannelFuture channelFuture = serverBootstrap.bind().sync(); | ||
System.out.println("The Echo Server start in : " + port); | ||
|
||
channelFuture.channel().closeFuture().sync(); | ||
|
||
} finally { | ||
group.shutdownGracefully().sync(); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/netty_demo/fileregion_demo/EchoServerHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package netty_demo.fileregion_demo; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.util.CharsetUtil; | ||
|
||
@ChannelHandler.Sharable | ||
public class EchoServerHandler extends ChannelInboundHandlerAdapter { | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | ||
ByteBuf buf = (ByteBuf) msg; | ||
System.out.println("The Time Server Received :" + buf.toString(CharsetUtil.UTF_8)); | ||
// System.out.println("The Time Server Received :" + buf.readInt()); | ||
ctx.write(buf); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) | ||
throws Exception { | ||
cause.printStackTrace(); | ||
ctx.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
adadadadadada你哈达铺打打打打拍的忙王牌阿冉冉忍耐欧菲光那烦恼复活节啊大家啊哦度奥断腕达瓦里玩电脑caacararawrwa | ||
awedeadawrar | ||
fafafwara | ||
f | ||
qwrarar | ||
fafafw |