I want send a BufferedImage object from client to server. But when i send the image the server immediately drop the client connection, without printing errors or exceptions.
MessagesWrapper:
@Data
public class MessageWrapper implements Serializable{
private static final long serialVersionUID = 1L;
String header;
Object content;
public MessageWrapper(String message, Object content) {
this.header = message;
this.content = content;
}
}
Sending an BufferedImage from client to server:
public class Client {
private static Channel channel;
private final String host;
private final int port;
public Client(String host, int port) {
this.host = host;
this.port = port;
}
public void run() throws InterruptedException, IOException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstarp = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.handler(new ClientInitalizer());
channel = bootstarp.connect(host, port).sync().channel();
BufferedImage originalImage = ImageIO.read(new File("c:\\test.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
Client.getChannel().write(new MessageWrapper("IMG", baos));
while (true) {
}
} finally {
group.shutdownGracefully();
}
}
public static Channel getChannel() {
return channel;
}
Server initalizer:
public class ServerInitaizer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
// pipeline.addLast("decoder", new ObjectDecoder(ClassResolvers.softCachingResolver(ClassLoader.getSystemClassLoader())));
pipeline.addLast("decoder", new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(ClassLoader.getSystemClassLoader())));
pipeline.addLast("encoder", new ObjectEncoder());
pipeline.addLast("handler", new ServerHandler());
}
}
Server handler:
public class ServerHandler extends SimpleChannelInboundHandler<Object> {
private static final Logger log = Logger.getLogger(Server.class.getName());
private static final ChannelGroup channels = new DefaultChannelGroup(
GlobalEventExecutor.INSTANCE);
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
log.info(ctx.toString());
Channel incoming = ctx.channel();
channels.add(incoming);
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
Channel incoming = null;
log.info(ctx.toString());
try {
incoming = ctx.channel();
} finally {
channels.remove(incoming);
}
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg)
throws Exception {
log.info(msg.toString());
}
}
Server.java:
public class Server {
private static final Logger log = Logger.getLogger(Server.class.getName());
private final int port;
public Server(int port) {
this.port = port;
}
public void run() throws InterruptedException {
EventLoopGroup mainGroup = new NioEventLoopGroup();
EventLoopGroup slaveGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstarp = new ServerBootstrap()
.group(mainGroup, slaveGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ServerInitaizer());
bootstarp.bind(port).sync().channel().closeFuture().sync();
} finally {
mainGroup.shutdownGracefully();
slaveGroup.shutdownGracefully();
}
}
}
Is there any idea what is the root of the problem?
Aucun commentaire:
Enregistrer un commentaire