Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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
Java netty错误:InvalidClassException:无法读取类描述符_Java_Multithreading_Sockets_Io_Netty - Fatal编程技术网

Java netty错误:InvalidClassException:无法读取类描述符

Java netty错误:InvalidClassException:无法读取类描述符,java,multithreading,sockets,io,netty,Java,Multithreading,Sockets,Io,Netty,在客户端和服务器之间发送、接收和转换对象的正确习惯用法是什么?我从一个简单的例子开始,我只想在客户端和服务器之间发送特定的类型 服务器输出: BUILD SUCCESSFUL Total time: 3 seconds Jul 26, 2014 3:36:22 AM io.netty.handler.logging.LoggingHandler channelRegistered INFO: [id: 0xefbb5b05] REGISTERED Jul 26, 2014 3:36:22 AM

在客户端和服务器之间发送、接收和转换对象的正确习惯用法是什么?我从一个简单的例子开始,我只想在客户端和服务器之间发送特定的类型

服务器输出:

BUILD SUCCESSFUL
Total time: 3 seconds
Jul 26, 2014 3:36:22 AM io.netty.handler.logging.LoggingHandler channelRegistered
INFO: [id: 0xefbb5b05] REGISTERED
Jul 26, 2014 3:36:22 AM io.netty.handler.logging.LoggingHandler bind
INFO: [id: 0xefbb5b05] BIND(0.0.0.0/0.0.0.0:4454)
Jul 26, 2014 3:36:22 AM io.netty.handler.logging.LoggingHandler channelActive
INFO: [id: 0xefbb5b05, /0:0:0:0:0:0:0:0:4454] ACTIVE
Jul 26, 2014 3:36:32 AM io.netty.handler.logging.LoggingHandler logMessage
INFO: [id: 0xefbb5b05, /0:0:0:0:0:0:0:0:4454] RECEIVED: [id: 0xabdeec06, /127.0.0.1:59934 => /127.0.0.1:4454]
Jul 26, 2014 3:36:32 AM net.bounceme.dur.netty.ServerHandler exceptionCaught
SEVERE: io.netty.handler.codec.DecoderException: java.io.InvalidClassException: failed to read class descriptor
Jul 26, 2014 3:36:32 AM net.bounceme.dur.netty.ServerHandler channelReadComplete
INFO: finished reading..?
^Cthufir@dur:~/NetBeansProjects/AgentServer$ 
thufir@dur:~/NetBeansProjects/AgentServer$ 
客户端输出:

BUILD SUCCESSFUL
Total time: 3 seconds
Jul 26, 2014 3:36:32 AM net.bounceme.dur.client.netty.ClientHandler channelActive
INFO: 

id  0
phone   0
title   null
state   undefined
thufir@dur:~/NetBeansProjects/AgentClient$ 
服务器处理程序:

package net.bounceme.dur.netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.logging.Logger;
import net.bounceme.dur.jdbc.Title;

/**
 * Handles both client-side and server-side handler depending on which
 * constructor was called.
 */
public class ServerHandler extends ChannelInboundHandlerAdapter {

    private static final Logger log = Logger.getLogger(ServerHandler.class.getName());
    private RecordsQueueWrapper q = null;

    public ServerHandler(RecordsQueueWrapper q) {
        this.q = q;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object obj) {
        Title receivedTitle = (Title) obj;
        log.info(receivedTitle.toString());
        Title nextTitle = q.pop();
        ctx.write(nextTitle);
        log.info("..channelRead");
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        log.info("finished reading..?");
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        log.severe(cause.toString());
        ctx.close();
    }
}
ClientHandler:

package net.bounceme.dur.client.netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.logging.Logger;
import net.bounceme.dur.client.jdbc.Title;

public class ClientHandler extends ChannelInboundHandlerAdapter {

    private static final Logger log = Logger.getLogger(ClientHandler.class.getName());

    public ClientHandler() {
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        Title firstTitle = new Title();
        log.info(firstTitle.toString());
        ctx.writeAndFlush(firstTitle);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        try {
            Title t = (Title) msg;
            log.info(msg.toString());
            ctx.write(t);
        } catch (ClassCastException cce) {  //????
            log.warning(cce.toString());
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx
    ) {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause
    ) {
        log.severe(cause.toString());
        ctx.close();
    }
}
服务器:

package net.bounceme.dur.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import java.security.cert.CertificateException;
import java.util.logging.Logger;
import javax.net.ssl.SSLException;
import net.bounceme.dur.jdbc.Title;

public final class Server {

    private static final Logger log = Logger.getLogger(Server.class.getName());

    public static void main(String[] args) throws Exception {
        MyProps p = new MyProps();
        int port = p.getServerPort();
        RecordsQueueWrapper q = new RecordsQueueWrapper();
        q.init(99);
        new Server().startServer(port, false, q);
    }

    private void startServer(int port, boolean ssl, final RecordsQueueWrapper q) throws CertificateException, SSLException, InterruptedException {
        final SslContext sslCtx;
        if (ssl) {
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
        } else {
            sslCtx = null;
        }
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            if (sslCtx != null) {
                                p.addLast(sslCtx.newHandler(ch.alloc()));
                            }
                            p.addLast(
                                    new ObjectEncoder(),
                                    new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(Title.class.getClassLoader())),
                            //new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                            new ServerHandler(q)
                            );
                        }
                    });

            b.bind(port).sync().channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
package net.bounceme.dur.netty;
导入io.netty.bootstrap.ServerBootstrap;
导入io.netty.channel.ChannelInitializer;
导入io.netty.channel.ChannelPipeline;
导入io.netty.channel.EventLoopGroup;
导入io.netty.channel.nio.NioEventLoopGroup;
导入io.netty.channel.socket.SocketChannel;
导入io.netty.channel.socket.nio.NioServerSocketChannel;
导入io.netty.handler.codec.serialization.ClassResolvers;
导入io.netty.handler.codec.serialization.ObjectDecoder;
导入io.netty.handler.codec.serialization.ObjectEncoder;
导入io.netty.handler.logging.LogLevel;
导入io.netty.handler.logging.LoggingHandler;
导入io.netty.handler.ssl.SslContext;
导入io.netty.handler.ssl.util.SelfSignedCertificate;
导入java.security.cert.CertificateException;
导入java.util.logging.Logger;
导入javax.net.ssl.SSLException;
导入net.bounceme.dur.jdbc.Title;
公共最终类服务器{
私有静态最终记录器log=Logger.getLogger(Server.class.getName());
公共静态void main(字符串[]args)引发异常{
MyProps p=新的MyProps();
int-port=p.getServerPort();
RecordsQueueWrapper q=新的RecordsQueueWrapper();
q、 init(99);
新服务器().startServer(端口,false,q);
}
私有void startServer(int端口、布尔ssl、最终记录squewrapper q)抛出CertificateException、SSlexException、InterruptedException{
最终SslContext sslCtx;
如果(ssl){
SelfSignedCertificate ssc=新的SelfSignedCertificate();
sslCtx=SslContext.newServerContext(ssc.certificate(),ssc.privateKey());
}否则{
sslCtx=null;
}
EventLoopGroup bossGroup=新的NioEventLoopGroup(1);
EventLoopGroup workerGroup=新的NioEventLoopGroup();
试一试{
ServerBootstrap b=新的ServerBootstrap();
b、 组(bossGroup、workerGroup)
.channel(NioServerSocketChannel.class)
.handler(新的LoggingHandler(LogLevel.INFO))
.childHandler(新的通道初始值设定项(){
@凌驾
public void initChannel(SocketChannel ch)引发异常{
ChannelPipeline p=通道管道();
如果(sslCtx!=null){
p、 addLast(sslCtx.newHandler(ch.alloc());
}
p、 addLast(
新的ObjectEncoder(),
新的ObjectDecoder(ClassResolver.weakCachingConcurrentResolver(Title.class.getClassLoader()),
//新的ObjectDecoder(ClassResolver.cacheDisabled(null)),
新服务器处理程序(q)
);
}
});
b、 绑定(端口).sync().channel().closeFuture().sync();
}最后{
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
客户:

package net.bounceme.dur.client.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import java.util.logging.Logger;
import javax.net.ssl.SSLException;
import net.bounceme.dur.client.jdbc.Title;

/**
 * Modification of {@link EchoClient} which utilizes Java object serialization.
 */
public final class Client {

    private static final Logger log = Logger.getLogger(Client.class.getName());
    static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));

    public Client() {
    }

    public void init() throws InterruptedException, SSLException {
        MyProps p = new MyProps();
        String host = p.getHost();
        int port = p.getServerPort();
        startClient(host, port, false);
    }

    private void startClient(final String host, final int port, final boolean SSL) throws SSLException, InterruptedException {
        // Configure SSL.
        final SslContext sslCtx;
        if (SSL) {
            sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
        } else {
            sslCtx = null;
        }

        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            if (sslCtx != null) {
                                p.addLast(sslCtx.newHandler(ch.alloc(), host, port));
                            }
                            p.addLast(
                                    new ObjectEncoder(),
                                    //new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                                    new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(Title.class.getClassLoader())),
                                    new ClientHandler());
                        }
                    });

            // Start the connection attempt.
            b.connect(host, port).sync().channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
}
package net.bounceme.dur.client.netty;
导入io.netty.bootstrap.bootstrap;
导入io.netty.channel.ChannelInitializer;
导入io.netty.channel.ChannelPipeline;
导入io.netty.channel.EventLoopGroup;
导入io.netty.channel.nio.NioEventLoopGroup;
导入io.netty.channel.socket.SocketChannel;
导入io.netty.channel.socket.nio.NioSocketChannel;
导入io.netty.handler.codec.serialization.ClassResolvers;
导入io.netty.handler.codec.serialization.ObjectDecoder;
导入io.netty.handler.codec.serialization.ObjectEncoder;
导入io.netty.handler.ssl.SslContext;
导入io.netty.handler.ssl.util.UnsecureTrustManagerFactory;
导入java.util.logging.Logger;
导入javax.net.ssl.SSLException;
导入net.bounceme.dur.client.jdbc.Title;
/**
*修改利用Java对象序列化的{@link EchoClient}。
*/
公共最终类客户端{
私有静态最终记录器log=Logger.getLogger(Client.class.getName());
静态final int SIZE=Integer.parseInt(System.getProperty(“SIZE”,“256”);
公共客户机(){
}
public void init()引发InterruptedException,ssLexException{
MyProps p=新的MyProps();
字符串host=p.getHost();
int-port=p.getServerPort();
startClient(主机、端口、错误);
}
私有void startClient(最终字符串主机、最终int端口、最终布尔SSL)引发SSLException、InterruptedException{
//配置SSL。
最终SslContext sslCtx;
如果(SSL){
sslCtx=SslContext.newClientContext(不安全的TrustManagerFactory.INSTANCE);
}否则{
sslCtx=null;
}
EventLoopGroup group=新建NioEventLoopGroup();
试一试{
引导b=新引导();
b、 组(组)
.channel(NioSocketChannel.class)
.handler(新的通道初始值设定项(){
@凌驾
public void initChannel(SocketChannel ch)引发异常{
ChannelPipeline p=通道管道();
如果(sslCtx!=null){
p、 addLast(sslCtx.newHandler(ch.alloc(),主机,端口));
}
p、 addLast(
新的ObjectEncoder(),
//新的ObjectDecoder(ClassResolver.cacheDisabled(null)),
新的ObjectDecoder(ClassResolver.weakCachingConcurrentResolver(Title.class.getClassLoader()),