0

对不起,如果我在浪费你们的时间,但这是我第一次使用 netty(jboss),我不知道我在做什么。这是我的服务器,我想写一个双精度:

public final class UpdateServer extends SimpleChannelHandler {

private static ChannelGroup channels;

private static final int PORT = 55555;
private static final double VERSION = 1.66;

public static void main(String[] args) {
    init();
}

public static final void init() {
    new UpdateServer();
}

/*
 * throws exeption so if cant handle channel server closes
 */
private UpdateServer() {
    ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());

    ServerBootstrap bootstrap = new ServerBootstrap(factory);

    ChannelPipeline pipeline = bootstrap.getPipeline();
    pipeline.addLast("handler", this);

    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.keepAlive", true);

    bootstrap.bind(new InetSocketAddress(PORT));
    System.out.println("Listening for connections on port: "+PORT);
}

@Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) {
    channels.add(e.getChannel());
}

@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {
    channels.remove(e.getChannel());
}

@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
    System.out.println("Connected!");
    e.getChannel().write(VERSION);
    ctx.getChannel().write(VERSION);
}

@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) {

}

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
    System.out.println("Message Recieved");
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent ee) throws Exception {

}

}

但是当我运行客户端时,我读取的双精度值为 0.0:

public class UpdateChecker {

public static void main(String[] args) throws Exception {
    SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 55555));
    socketChannel.configureBlocking(false);
    ByteBuffer buffer = ByteBuffer.allocateDirect(8);
    socketChannel.read(buffer); 
    double value = buffer.getDouble();
    System.out.println(value);
}

}

我不确定为什么它以 0.0 打印,因为那不是我写的,我该如何解决这个问题?

谢谢。

4

1 回答 1

2

尝试将服务器套接字显式绑定到“127.0.0.1”

于 2013-02-07T23:54:43.743 回答