0
votes

I'm working on a simple Application. I'm trying to understand my the exception, but i just can't.

How to reproduce:

  1. Starting Netty Server
  2. Connecting to Netty Server with Client -> Valid Response, Server + Client working fine.
  3. Client closed (0 Channels are active, proven by a debug thread)
  4. NEW Client tries to read -> Error: java.net.SocketException: Software caused connection abort: recv failed
  5. Restarting Server, goto 2.

Server source:

public class Server {

private final int port;

public Server(int port) {
    this.port = port;
}

public void run() throws Exception {
    final EventLoopGroup boss = new NioEventLoopGroup();
    final EventLoopGroup worker = new NioEventLoopGroup();
    try {
        final ServerBootstrap b = new ServerBootstrap();
        b.group(boss, worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new SocketChannelInitializer());

        final ChannelFuture f = b.bind(port).sync();
        final ChannelFuture c = f.channel().closeFuture();
        System.out.println("- DONE -");

        c.sync();
    } finally {
        worker.shutdownGracefully();
        boss.shutdownGracefully();
    }
}

public static void main(String[] args) throws Exception {
    int port;
    if (args.length > 0) {
        port = Integer.parseInt(args[0]);
    } else {
        port = 8080;
    }
    new Server(port).run();
}

}

My Initialisizer:

public static final String PACKET = "packet";
public static final String STRING_DECODER = "stringDecoder";
public static final String NETWORK_HANDLER = "networkHandler";

@Override
public void initChannel(SocketChannel ch) throws Exception {
    System.out.println("Creating new Channel!");
    final ChannelPipeline p = ch.pipeline();
    p.addLast(NETWORK_HANDLER, new NetworkHandler());
    p.addLast(STRING_DECODER, new StringDecoder(CharsetUtil.UTF_8));
    p.addLast(new Testdecoder());
    p.addLast(new ChatAdapter());
}

Server DOES process the request, but it cannot read a correct Message.

My Client:

public static void main(String[] args) throws IOException {
    final Socket s = new Socket();
    s.connect(new InetSocketAddress("localhost", 8080));
    final InputStream is = s.getInputStream();
    final OutputStream os = s.getOutputStream();
    os.write(0x0);
    os.write("username".getBytes(CharsetUtil.UTF_8));
    os.flush();
    System.out.println("!");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] b = new byte[4096];
    while (true) {
        int n = is.read(b);
        if (n < 0) {
            break;
        }
        baos.write(b, 0, n);
    }

    byte data[] = baos.toByteArray();
    System.out.println(new String(data, CharsetUtil.UTF_8));
    System.out.println("- DONE -");
    s.close();
}

Using Netty 4.0.23.Final

Example Application: https://github.com/BjoernAkAManf/Chat

Startup Server.main(String[] args) and run Client.main(String[] args) twice. You'll get a proper output first. 2nd run will fail. I tested anything i could possibly think of. Really appreciate help here. Thank you

2
Downvoting without any comment. That's pretty nice. I'm trying to debug the whole programm myself, working pretty hard for days - I don't mean to offend anyone, but if you downvote a question, atleast tell me the Reasons - This issue is easy to reproduce, just test it yourself. If the issue would be somehow related to my handlers, an exception would be thrown or printed somewhere. There has to be a flaw in my logic somewhere - Noone has ever reported this Issue before and i'm using stables here.manf

2 Answers

0
votes

Despite my first Idea this is not a 'real' server issue, nor is it an issue with netty. The Client uses oio (java.IO) Sockets to connect to a Nio Netty Server.

Changing NioEventLoopGroup to OioEventLoopGroup and NioServerSocketChannel to OioServerSocketChannel resolves this weird issue.

Furthermore one may change the Client to work with Nio. In the following Example i will use java.nio:

    final SocketChannel c = SocketChannel.open();
    c.connect(new InetSocketAddress("localhost", 8007));

    final ByteBuffer buf = ByteBuffer.allocate(5);

    // fill Buffer
    buf.put((byte) 0x0);
    buf.put("Test".getBytes(CharsetUtil.UTF_8));
    // Flip buffer
    buf.flip();

    // Write buffer to socket
    while (buf.hasRemaining()) {
        c.write(buf);
    }

    System.out.println("Reading ...");

    // Read 
    buf.flip();
    while (true) {
        int n = c.read(buf);
        if (n < 0) {
            break;
        }
        final String d = new String(buf.array());
        if(!d.isEmpty()) System.out.println("'" + d + "'");
        buf.clear();
    }

    // Finish Programm
    System.out.println("- DONE -");
    c.close();

That example works perfectly fine. Obviously the problem was right infront of my eyes.

Now it's fixed.

0
votes

This can be solved by providing Thread.sleep(time) before you close the server and start again.