I'm working on a simple Application. I'm trying to understand my the exception, but i just can't.
How to reproduce:
- Starting Netty Server
- Connecting to Netty Server with Client -> Valid Response, Server + Client working fine.
- Client closed (0 Channels are active, proven by a debug thread)
- NEW Client tries to read -> Error: java.net.SocketException: Software caused connection abort: recv failed
- 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