问题
How do I initiate an infinite loop between the client and server, to send and receive POJO PingPong
objects between the client and server?
When a connection is established, which seems to be happening, the client should send a PingPong
, and the server should reply with a PingPong
POJO as well. The client should (preferably) respond with another PingPong
, creating an infinite loop.
the server receives an object:
BUILD SUCCESSFUL
Total time: 2 seconds
Jul 20, 2014 2:43:17 PM io.netty.handler.logging.LoggingHandler channelRegistered
INFO: [id: 0x2cd55a8d] REGISTERED
Jul 20, 2014 2:43:17 PM io.netty.handler.logging.LoggingHandler bind
INFO: [id: 0x2cd55a8d] BIND(0.0.0.0/0.0.0.0:4454)
Jul 20, 2014 2:43:17 PM io.netty.handler.logging.LoggingHandler channelActive
INFO: [id: 0x2cd55a8d, /0:0:0:0:0:0:0:0:4454] ACTIVE
Jul 20, 2014 2:43:24 PM io.netty.handler.logging.LoggingHandler logMessage
INFO: [id: 0x2cd55a8d, /0:0:0:0:0:0:0:0:4454] RECEIVED: [id: 0xe7c06459, /127.0.0.1:33182 => /127.0.0.1:4454]
^Cthufir@dur:~/NetBeansProjects/NettyServer$
thufir@dur:~/NetBeansProjects/NettyServer$
which was sent by the client:
BUILD SUCCESSFUL
Total time: 2 seconds
Jul 20, 2014 2:43:24 PM io.netty.handler.logging.LoggingHandler channelRegistered
INFO: [id: 0x0f1fc251] REGISTERED
Jul 20, 2014 2:43:24 PM io.netty.handler.logging.LoggingHandler connect
INFO: [id: 0x0f1fc251] CONNECT(localhost/127.0.0.1:4454, null)
Jul 20, 2014 2:43:24 PM io.netty.handler.logging.LoggingHandler channelActive
INFO: [id: 0x0f1fc251, /127.0.0.1:33182 => localhost/127.0.0.1:4454] ACTIVE
^Cthufir@dur:~/NetBeansProjects/NettyClient$
thufir@dur:~/NetBeansProjects/NettyClient$
server code:
package net.bounceme.dur.netty;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.net.Socket;
import java.util.logging.Logger;
@Sharable
public class PingPongServerHandler extends SimpleChannelInboundHandler<PingPong> {
private static final Logger log = Logger.getLogger(PingPongServerHandler.class.getName());
protected Socket socket = null;
@Override
protected void channelRead0(ChannelHandlerContext chc, PingPong pingPong) throws Exception {
log.info(pingPong.getClass().getSimpleName());
chc.writeAndFlush(new PingPong());
}
}
client code:
package net.bounceme.dur.netty;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.logging.Logger;
public class PingPongClientHandler extends SimpleChannelInboundHandler<PingPong> {
private static final Logger log = Logger.getLogger(PingPongClientHandler.class.getName());
public PingPongClientHandler() {
}
@Override
protected void channelRead0(ChannelHandlerContext chc, PingPong pingPong) throws Exception {
log.info("reading...");
chc.writeAndFlush(new PingPong());
}
}
adapted from:
https://github.com/netty/netty/tree/4.0/example/src/main/java/io/netty/example/objectecho
branch 4.0
see also:
https://community.jboss.org/wiki/NettyExampleOfPingPongUsingObject
which uses 3.x and not 4.x, and is overly complex for my purposes.
server:
src/
├── net
│ └── bounceme
│ └── dur
│ └── netty
│ ├── EchoServer.java
│ ├── MyProps.java
│ ├── PingPong.java
│ └── PingPongServerHandler.java
└── server.properties
client:
src
├── net
│ └── bounceme
│ └── dur
│ └── netty
│ ├── EchoClient.java
│ ├── MyProps.java
│ ├── PingPongClientHandler.java
│ └── PingPong.java
└── server.properties
I'm trying to adapt the Echo Netty example to use POJO's instead of ByteBuf.
来源:https://stackoverflow.com/questions/24855307/simplest-possible-pojo-echo-with-netty