File tree Expand file tree Collapse file tree 5 files changed +92
-5
lines changed
07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx Expand file tree Collapse file tree 5 files changed +92
-5
lines changed Original file line number Diff line number Diff line change 1- package io .kimmking .rpcfx .utils ;
1+ package io .kimmking .rpcfx .client ;
22
33import io .kimmking .rpcfx .handler .ClientHandler ;
44import io .netty .bootstrap .Bootstrap ;
1010import io .netty .channel .socket .nio .NioSocketChannel ;
1111import io .netty .util .CharsetUtil ;
1212
13- public class NettyClientUtils {
13+ public class NettyClient {
1414
1515 private final String host ;
1616
@@ -19,7 +19,7 @@ public class NettyClientUtils {
1919 private final String req ;
2020
2121
22- public NettyClientUtils (String host , Integer port , String req ) {
22+ public NettyClient (String host , Integer port , String req ) {
2323 this .host = host ;
2424 this .port = port ;
2525 this .req = req ;
@@ -50,4 +50,8 @@ protected void initChannel(SocketChannel socketChannel) throws Exception {
5050 return resp ;
5151 }
5252
53+ public static void main (String [] args ) throws InterruptedException {
54+ new NettyClient ("127.0.0.1" , 8888 , "hello netty" ).start ();
55+ }
56+
5357}
Original file line number Diff line number Diff line change 88import io .kimmking .rpcfx .param .RpcfxRequest ;
99import io .kimmking .rpcfx .param .RpcfxResponse ;
1010import io .kimmking .rpcfx .utils .ClientUtils ;
11- import io .kimmking .rpcfx .utils .NettyClientUtils ;
1211import io .kimmking .rpcfx .utils .XStreamUtils ;
1312import okhttp3 .MediaType ;
1413import okhttp3 .Request ;
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ public ClientHandler(String req) {
1919 @ Override
2020 protected void channelRead0 (ChannelHandlerContext channelHandlerContext , ByteBuf byteBuf ) throws Exception {
2121
22- System .out .println (byteBuf .toString (CharsetUtil .UTF_8 ));
22+ System .out .println ("Client receiver:" + byteBuf .toString (CharsetUtil .UTF_8 ));
2323
2424 }
2525
Original file line number Diff line number Diff line change 1+ package io .kimmking .rpcfx .handler ;
2+
3+ import io .netty .buffer .ByteBuf ;
4+ import io .netty .buffer .Unpooled ;
5+ import io .netty .channel .*;
6+ import io .netty .util .CharsetUtil ;
7+
8+ @ ChannelHandler .Sharable
9+ public class ServerHandler extends ChannelInboundHandlerAdapter {
10+
11+ /**
12+ * 读取消息,写到回给发送者
13+ * @param ctx
14+ * @param msg
15+ * @throws Exception
16+ */
17+ @ Override
18+ public void channelRead (ChannelHandlerContext ctx , Object msg ) throws Exception {
19+ ByteBuf buf = (ByteBuf ) msg ;
20+ System .out .println ("Server receiver:" + buf .toString (CharsetUtil .UTF_8 ));
21+ ctx .write (buf );
22+ }
23+
24+
25+ @ Override
26+ public void channelReadComplete (ChannelHandlerContext ctx ) throws Exception {
27+ ctx .writeAndFlush (Unpooled .EMPTY_BUFFER ).addListener (ChannelFutureListener .CLOSE );
28+ }
29+
30+ @ Override
31+ public void exceptionCaught (ChannelHandlerContext ctx , Throwable cause ) throws Exception {
32+ cause .printStackTrace ();
33+ ctx .close ();
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ package io .kimmking .rpcfx .server ;
2+
3+ import io .kimmking .rpcfx .handler .ServerHandler ;
4+ import io .netty .bootstrap .ServerBootstrap ;
5+ import io .netty .channel .ChannelFuture ;
6+ import io .netty .channel .ChannelInitializer ;
7+ import io .netty .channel .EventLoopGroup ;
8+ import io .netty .channel .nio .NioEventLoopGroup ;
9+ import io .netty .channel .socket .SocketChannel ;
10+ import io .netty .channel .socket .nio .NioServerSocketChannel ;
11+
12+ public class NettyServer {
13+
14+ private final Integer port ;
15+
16+ public NettyServer (Integer port ) {
17+ this .port = port ;
18+ }
19+
20+ public void start () {
21+
22+ EventLoopGroup group = new NioEventLoopGroup ();
23+ try {
24+ ServerBootstrap bootstrap = new ServerBootstrap ();
25+ bootstrap .group (group ).
26+ channel (NioServerSocketChannel .class ).
27+ localAddress (port ).
28+ childHandler (new ChannelInitializer <SocketChannel >() {
29+ @ Override
30+ protected void initChannel (SocketChannel socketChannel ) throws Exception {
31+ socketChannel .pipeline ().addLast (new ServerHandler ());
32+ }
33+ });
34+ ChannelFuture future = bootstrap .bind ().sync ();
35+ future .channel ().closeFuture ().sync ();
36+ } catch (InterruptedException e ) {
37+ e .printStackTrace ();
38+ } finally {
39+ group .shutdownGracefully ();
40+ }
41+
42+
43+ }
44+
45+ public static void main (String [] args ) {
46+ new NettyServer (8888 ).start ();
47+ }
48+
49+ }
You can’t perform that action at this time.
0 commit comments