私は単純なアプリケーションに取り組んでいます。私は自分の例外を理解しようとしていますが、理解できません。
再現方法:
サーバーソース:
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();
}
}
私のイニシャライザー:
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());
}
サーバーはリクエストを処理しますが、正しいメッセージを読み取ることができません。
私の顧客:
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();
}
Netty4.0.23.Finalの使用
アプリケーションの例:https://github.com/BjoernAkAManf/Chat
Server.main(String [] args)を起動し、Client.main(String [] args)を2回実行します。最初に適切な出力が得られます。2回目の実行は失敗します。考えられることは何でもテストしました。ここで本当に助けてくれてありがとう。ありがとうございました
私の最初のアイデアにもかかわらず、これは「実際の」サーバーの問題ではなく、nettyの問題でもありません。クライアントはoio(java.IO)ソケットを使用してNioNettyサーバーに接続します。
NioEventLoopGroupをOioEventLoopGroupに変更し、NioServerSocketChannelをOioServerSocketChannelに変更すると、この奇妙な問題が解決されます。
さらに、Nioで動作するようにクライアントを変更することもできます。次の例では、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();
その例は完全にうまく機能します。明らかに、問題は私の目の前にありました。
今では修正されました。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加