Skip to content

Commit 5984fdc

Browse files
committed
修改netty版本,增加测试代码
1 parent 4dad3ae commit 5984fdc

6 files changed

Lines changed: 238 additions & 238 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<dependency>
3636
<groupId>io.netty</groupId>
3737
<artifactId>netty-all</artifactId> <!-- Use 'netty-all' for 4.0 or above -->
38-
<version>5.0.0.Alpha1</version>
38+
<version>5.0.0.Alpha2</version>
3939
</dependency>
4040

4141
<dependency>
Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
1-
//package netty;
2-
//
3-
//import io.netty.bootstrap.ServerBootstrap;
4-
//import io.netty.channel.*;
5-
//import io.netty.channel.nio.NioEventLoopGroup;
6-
//import io.netty.channel.socket.SocketChannel;
7-
//import io.netty.channel.socket.nio.NioServerSocketChannel;
8-
//import io.netty.handler.codec.string.StringDecoder;
9-
//import io.netty.handler.codec.string.StringEncoder;
10-
//import io.netty.util.CharsetUtil;
11-
//
12-
///**
13-
// * Created with IntelliJ IDEA.
14-
// * User: liukunyang
15-
// * Date: 14-3-28
16-
// * Time: 下午6:02
17-
// * To change this template use File | Settings | File Templates.
18-
// */
19-
///**
20-
// * Discards any incoming data.
21-
// */
22-
//public class DiscardServer {
23-
//
24-
// private int port;
25-
//
26-
// public DiscardServer(int port) {
27-
// this.port = port;
28-
// }
29-
//
30-
// public void run() throws Exception {
31-
//
32-
// EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
33-
// EventLoopGroup workerGroup = new NioEventLoopGroup();
34-
// try {
35-
// ServerBootstrap b = new ServerBootstrap(); // (2)
36-
// b.group(bossGroup, workerGroup)
37-
// .channel(NioServerSocketChannel.class) // (3)
38-
// .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
39-
// @Override
40-
// public void initChannel(SocketChannel ch) throws Exception {
41-
// ChannelPipeline pipeline = ch.pipeline();
42-
// pipeline.addLast(new DiscardServerHandler());
43-
// pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
44-
// pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
45-
// }
46-
// })
47-
// .option(ChannelOption.SO_BACKLOG, 128) // (5)
48-
// .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
49-
//
50-
// // Bind and start to accept incoming connections.
51-
// ChannelFuture f = b.bind(port).sync(); // (7)
52-
//
53-
// // Wait until the server socket is closed.
54-
// // In this example, this does not happen, but you can do that to gracefully
55-
// // shut down your server.
56-
// f.channel().closeFuture().sync();
57-
// } finally {
58-
// workerGroup.shutdownGracefully();
59-
// bossGroup.shutdownGracefully();
60-
// }
61-
// }
62-
//
63-
// public static void main(String[] args) throws Exception {
64-
// int port;
65-
// if (args.length > 0) {
66-
// port = Integer.parseInt(args[0]);
67-
// } else {
68-
// port = 8080;
69-
// }
70-
// new DiscardServer(port).run();
71-
// }
72-
//}
1+
package netty;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.*;
5+
import io.netty.channel.nio.NioEventLoopGroup;
6+
import io.netty.channel.socket.SocketChannel;
7+
import io.netty.channel.socket.nio.NioServerSocketChannel;
8+
import io.netty.handler.codec.string.StringDecoder;
9+
import io.netty.handler.codec.string.StringEncoder;
10+
import io.netty.util.CharsetUtil;
11+
12+
/**
13+
* Created with IntelliJ IDEA.
14+
* User: liukunyang
15+
* Date: 14-3-28
16+
* Time: 下午6:02
17+
* To change this template use File | Settings | File Templates.
18+
*/
19+
/**
20+
* Discards any incoming data.
21+
*/
22+
public class DiscardServer {
23+
24+
private int port;
25+
26+
public DiscardServer(int port) {
27+
this.port = port;
28+
}
29+
30+
public void run() throws Exception {
31+
32+
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
33+
EventLoopGroup workerGroup = new NioEventLoopGroup();
34+
try {
35+
ServerBootstrap b = new ServerBootstrap(); // (2)
36+
b.group(bossGroup, workerGroup)
37+
.channel(NioServerSocketChannel.class) // (3)
38+
.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
39+
@Override
40+
public void initChannel(SocketChannel ch) throws Exception {
41+
ChannelPipeline pipeline = ch.pipeline();
42+
pipeline.addLast(new DiscardServerHandler());
43+
pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
44+
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
45+
}
46+
})
47+
.option(ChannelOption.SO_BACKLOG, 128) // (5)
48+
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
49+
50+
// Bind and start to accept incoming connections.
51+
ChannelFuture f = b.bind(port).sync(); // (7)
52+
53+
// Wait until the server socket is closed.
54+
// In this example, this does not happen, but you can do that to gracefully
55+
// shut down your server.
56+
f.channel().closeFuture().sync();
57+
} finally {
58+
workerGroup.shutdownGracefully();
59+
bossGroup.shutdownGracefully();
60+
}
61+
}
62+
63+
public static void main(String[] args) throws Exception {
64+
int port;
65+
if (args.length > 0) {
66+
port = Integer.parseInt(args[0]);
67+
} else {
68+
port = 8080;
69+
}
70+
new DiscardServer(port).run();
71+
}
72+
}
Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
1-
//package netty;
2-
//
3-
//import io.netty.buffer.ByteBuf;
4-
//import io.netty.channel.*;
5-
//import io.netty.util.ReferenceCountUtil;
6-
//
7-
///**
8-
// * Created with IntelliJ IDEA.
9-
// * User: liukunyang
10-
// * Date: 14-3-31
11-
// * Time: 下午1:27
12-
// * To change this template use File | Settings | File Templates.
13-
// */
14-
//public class DiscardServerHandler extends ChannelHandlerAdapter { // (1)
15-
//
16-
// @Override
17-
// public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
18-
// ByteBuf in = (ByteBuf) msg;
19-
// try {
20-
// while (in.isReadable()) { // (1)
21-
// System.out.print((char) in.readByte());
22-
// System.out.flush();
1+
package netty;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.channel.*;
5+
import io.netty.util.ReferenceCountUtil;
6+
7+
/**
8+
* Created with IntelliJ IDEA.
9+
* User: liukunyang
10+
* Date: 14-3-31
11+
* Time: 下午1:27
12+
* To change this template use File | Settings | File Templates.
13+
*/
14+
public class DiscardServerHandler extends ChannelHandlerAdapter { // (1)
15+
16+
@Override
17+
public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
18+
ByteBuf in = (ByteBuf) msg;
19+
try {
20+
while (in.isReadable()) { // (1)
21+
System.out.print((char) in.readByte());
22+
System.out.flush();
23+
}
24+
25+
26+
ctx.write("hello this is server"); // (1)
27+
ctx.flush(); // (2)
28+
29+
} finally {
30+
ReferenceCountUtil.release(msg); // (2)
31+
}
32+
}
33+
34+
35+
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
36+
ctx.write("hello this is server"); // (1)
37+
ctx.flush(); // (2)
38+
}
39+
40+
@Override
41+
public void channelActive( final ChannelHandlerContext ctx) throws Exception {
42+
final ByteBuf time = ctx.alloc().buffer(50); // (2)
43+
44+
time.writeBytes("this is server".getBytes());
45+
ctx.write(time);
46+
ctx.flush();
47+
// final ChannelFuture f = ctx.writeAndFlush(time); // (3)
48+
// f.addListener(new ChannelFutureListener() {
49+
// @Override
50+
// public void operationComplete(ChannelFuture future) {
51+
// assert f == future;
52+
// ctx.close();
2353
// }
24-
//
25-
//
26-
// ctx.write("hello this is server"); // (1)
27-
// ctx.flush(); // (2)
28-
//
29-
// } finally {
30-
// ReferenceCountUtil.release(msg); // (2)
31-
// }
32-
// }
33-
//
34-
//
35-
// public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
36-
// ctx.write("hello this is server"); // (1)
37-
// ctx.flush(); // (2)
38-
// }
39-
//
40-
// @Override
41-
// public void channelActive( final ChannelHandlerContext ctx) throws Exception {
42-
// final ByteBuf time = ctx.alloc().buffer(50); // (2)
43-
//
44-
// time.writeBytes("this is server".getBytes());
45-
// ctx.write(time);
46-
// ctx.flush();
47-
//// final ChannelFuture f = ctx.writeAndFlush(time); // (3)
48-
//// f.addListener(new ChannelFutureListener() {
49-
//// @Override
50-
//// public void operationComplete(ChannelFuture future) {
51-
//// assert f == future;
52-
//// ctx.close();
53-
//// }
54-
//// }); // (4)
55-
// }
56-
//
57-
// @Override
58-
// public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
59-
// // Close the connection when an exception is raised.
60-
// cause.printStackTrace();
61-
// ctx.close();
62-
// }
63-
//}
54+
// }); // (4)
55+
}
56+
57+
@Override
58+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
59+
// Close the connection when an exception is raised.
60+
cause.printStackTrace();
61+
ctx.close();
62+
}
63+
}
Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
//package netty;
2-
//
3-
//import io.netty.bootstrap.Bootstrap;
4-
//import io.netty.channel.*;
5-
//import io.netty.channel.nio.NioEventLoopGroup;
6-
//import io.netty.channel.socket.SocketChannel;
7-
//import io.netty.channel.socket.nio.NioSocketChannel;
8-
//import io.netty.handler.codec.string.StringDecoder;
9-
//import io.netty.handler.codec.string.StringEncoder;
10-
//import io.netty.util.CharsetUtil;
11-
//
12-
///**
13-
// * Created with IntelliJ IDEA.
14-
// * User: liukunyang
15-
// * Date: 14-3-31
16-
// * Time: 下午1:31
17-
// * To change this template use File | Settings | File Templates.
18-
// */
19-
//public class TimeClient {
20-
// public static void main(String[] args) throws Exception {
21-
// String host = "127.0.0.1";
22-
// EventLoopGroup workerGroup = new NioEventLoopGroup();
23-
//
24-
// try {
25-
// Bootstrap b = new Bootstrap(); // (1)
26-
// b.group(workerGroup); // (2)
27-
// b.channel(NioSocketChannel.class); // (3)
28-
// b.option(ChannelOption.SO_KEEPALIVE, true); // (4)
29-
// b.handler(new ChannelInitializer<SocketChannel>() {
30-
// @Override
31-
// public void initChannel(SocketChannel ch) throws Exception {
32-
// ChannelPipeline pipeline = ch.pipeline();
33-
// pipeline.addLast(new TimeClientHandler());
34-
// pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
35-
// pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
36-
//
37-
// }
38-
// });
39-
//
40-
// // Start the client.
41-
// ChannelFuture f = b.connect(host, 8999).sync(); // (5)
42-
//
43-
//// f.channel().write("hello this is client");
44-
// // Wait until the connection is closed.
45-
// f.channel().closeFuture().sync();
46-
// } finally {
47-
// workerGroup.shutdownGracefully();
48-
// }
49-
// }
50-
//}
1+
package netty;
2+
3+
import io.netty.bootstrap.Bootstrap;
4+
import io.netty.channel.*;
5+
import io.netty.channel.nio.NioEventLoopGroup;
6+
import io.netty.channel.socket.SocketChannel;
7+
import io.netty.channel.socket.nio.NioSocketChannel;
8+
import io.netty.handler.codec.string.StringDecoder;
9+
import io.netty.handler.codec.string.StringEncoder;
10+
import io.netty.util.CharsetUtil;
11+
12+
/**
13+
* Created with IntelliJ IDEA.
14+
* User: liukunyang
15+
* Date: 14-3-31
16+
* Time: 下午1:31
17+
* To change this template use File | Settings | File Templates.
18+
*/
19+
public class TimeClient {
20+
public static void main(String[] args) throws Exception {
21+
String host = "127.0.0.1";
22+
EventLoopGroup workerGroup = new NioEventLoopGroup();
23+
24+
try {
25+
Bootstrap b = new Bootstrap(); // (1)
26+
b.group(workerGroup); // (2)
27+
b.channel(NioSocketChannel.class); // (3)
28+
b.option(ChannelOption.SO_KEEPALIVE, true); // (4)
29+
b.handler(new ChannelInitializer<SocketChannel>() {
30+
@Override
31+
public void initChannel(SocketChannel ch) throws Exception {
32+
ChannelPipeline pipeline = ch.pipeline();
33+
pipeline.addLast(new TimeClientHandler());
34+
pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
35+
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
36+
37+
}
38+
});
39+
40+
// Start the client.
41+
ChannelFuture f = b.connect(host, 8999).sync(); // (5)
42+
43+
// f.channel().write("hello this is client");
44+
// Wait until the connection is closed.
45+
f.channel().closeFuture().sync();
46+
} finally {
47+
workerGroup.shutdownGracefully();
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)