I've used some examples to send and receive strings and they have worked perfectly. The thing is I have tried to adapt the code(reading the api and examples) to send serializable objects and they dont work. I don't get any error message, channel read method is never invoked so server never gets the message. I read it could be related to some kind of delimeter, but I can't find any clue regarding it
Here is the code, i won't include handler added and removed because they work
Server initiaizer
protected void initChannel(SocketChannel arg0) throws Exception {
ChannelPipeline pipeline = arg0.pipeline();
pipeline.addLast("decoder", new ObjectDecoder(null));
pipeline.addLast("encoder", new ObjectEncoder());
pipeline.addLast("handler", new ChatServerHandler());
}
Channel read
public void channelRead0(ChannelHandlerContext arg0, Message message)
throws Exception {
Channel incoming = arg0.channel();
System.out.println("received "+ message.getContent() + "from " + message.getSender() + "\r\n" );
}
Run server
public void run() throws InterruptedException{
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try{
ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChatServerInitializer());
bootstrap.bind(port).sync().channel().closeFuture().sync();
}
And here is the client
protected void initChannel(SocketChannel arg0) throws Exception {
ChannelPipeline pipeline = arg0.pipeline();
pipeline.addLast("decoder", new ObjectDecoder(null));
pipeline.addLast("encoder", new ObjectEncoder());
pipeline.addLast("handler", new ChatClientHandler());
}
and the main (server says client connected)
public void run(){
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.handler(new ChatClientInitializer());
Channel channel = bootstrap.connect(host,port).sync().channel();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Message message = new Message();
message.setReceiver("user");
message.setSender("user 2");
try {
message.setContent(in.readLine());
channel.write(message);
System.out.println("Sent " + System.currentTimeMillis());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Last, the message class without get/set
public class Message implements Serializable {
private static final long serialVersionUID = 1L;
private String sender;
private String receiver;
private String content;
public Message(String sender, String receiver, String content){
this.setSender(sender);
this.setReceiver(receiver);
this.setContent(content);
}
Thanks a lot for your time
Check the ChannelFuture returned from the write operation. I bet it was failed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.