Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
普通TCP客户端和;使用Java的Netty服务器_Java_Sockets_Tcp_Protocol Buffers_Netty - Fatal编程技术网

普通TCP客户端和;使用Java的Netty服务器

普通TCP客户端和;使用Java的Netty服务器,java,sockets,tcp,protocol-buffers,netty,Java,Sockets,Tcp,Protocol Buffers,Netty,我正在尝试使用Java中的Protobuf消息交换格式在普通TCP客户机和Netty服务器之间创建TCP套接字连接,但它不起作用。当我使用Netty客户端(而不是TCP客户端)和Netty服务器时,它就可以工作了 在Netty服务器端,在ServerHandler类中,我得到对象“msg”作为类型“PooledUnsafeDirectByteBuf”。现在,当我尝试将其转换为我的自定义Protobuf对象时,它失败了,错误为“java.lang.ClassCastException:class

我正在尝试使用Java中的Protobuf消息交换格式在普通TCP客户机和Netty服务器之间创建TCP套接字连接,但它不起作用。当我使用Netty客户端(而不是TCP客户端)和Netty服务器时,它就可以工作了

在Netty服务器端,在ServerHandler类中,我得到对象“msg”作为类型“PooledUnsafeDirectByteBuf”。现在,当我尝试将其转换为我的自定义Protobuf对象时,它失败了,错误为“java.lang.ClassCastException:class io.netty.buffer.PooledUnsafeDirectByteBuf无法转换为class ProtoModel”

public class ServerHandler extends ChannelInboundHandlerAdapter  {
---
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
--- 
客户端代码

Socket clientConnection = new Socket("localhost",SERVER_PORT);
ObjectOutputStream outToServer = new ObjectOutputStream(clientConnection.getOutputStream());
ProtoModel.writeTo(outToServer); //ProtoModel is the protobuf class
我认为这与TCP客户端的Protobuf消息编码和Netty服务器端的解码有关。当我使用Netty客户端(与普通TCP客户端相反)时,相同的服务器代码也可以工作

净客户代码

EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class).handler(new ClientInitializer());
Channel ch = bootstrap.connect("localhost",SERVER_PORT).sync().channel();
ChannelFuture lastWriteFuture = ch.writeAndFlush(ProtoModel);
lastWriteFuture.channel().closeFuture().sync();
public void channelRead(ChannelHandlerContext ctx, Object msg) {
            ByteBuf buf = (ByteBuf) msg;
            byte[] req = new byte[buf.readableBytes()];
            buf.readBytes(req);
            ProtobufModel obj = ProtobufModel.parseFrom(ByteString.copyFrom(req));
}
SocketChannel channel = SocketChannel
                    .open(new InetSocketAddress(InetAddress.getByName("localhost"), SERVER_PORT));
          ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
          byteBuffer.put(ProtobufModel.toByteArray());
          byteBuffer.flip();
          channel.write(byteBuffer);

请让我知道,如果任何其他输入需要从我的结束。非常感谢。

我能够解决这个问题。我在下面发布解决方案

服务器代码

EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class).handler(new ClientInitializer());
Channel ch = bootstrap.connect("localhost",SERVER_PORT).sync().channel();
ChannelFuture lastWriteFuture = ch.writeAndFlush(ProtoModel);
lastWriteFuture.channel().closeFuture().sync();
public void channelRead(ChannelHandlerContext ctx, Object msg) {
            ByteBuf buf = (ByteBuf) msg;
            byte[] req = new byte[buf.readableBytes()];
            buf.readBytes(req);
            ProtobufModel obj = ProtobufModel.parseFrom(ByteString.copyFrom(req));
}
SocketChannel channel = SocketChannel
                    .open(new InetSocketAddress(InetAddress.getByName("localhost"), SERVER_PORT));
          ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
          byteBuffer.put(ProtobufModel.toByteArray());
          byteBuffer.flip();
          channel.write(byteBuffer);
对象“msg”在服务器端以ByteBuf格式接收。然后需要将“ByteBuf”格式转换为“ByteString”,最后可以从“ByteString”格式检索ProtobufModel

客户端代码

EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class).handler(new ClientInitializer());
Channel ch = bootstrap.connect("localhost",SERVER_PORT).sync().channel();
ChannelFuture lastWriteFuture = ch.writeAndFlush(ProtoModel);
lastWriteFuture.channel().closeFuture().sync();
public void channelRead(ChannelHandlerContext ctx, Object msg) {
            ByteBuf buf = (ByteBuf) msg;
            byte[] req = new byte[buf.readableBytes()];
            buf.readBytes(req);
            ProtobufModel obj = ProtobufModel.parseFrom(ByteString.copyFrom(req));
}
SocketChannel channel = SocketChannel
                    .open(new InetSocketAddress(InetAddress.getByName("localhost"), SERVER_PORT));
          ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
          byteBuffer.put(ProtobufModel.toByteArray());
          byteBuffer.flip();
          channel.write(byteBuffer);
在客户端,ProtobufModel转换为ByteBuffer并传递给服务器