Home > other >  Netty server sends a 1 MB data, the client receives the incomplete, only a dozen KB
Netty server sends a 1 MB data, the client receives the incomplete, only a dozen KB

Time:10-05

I am in the most simple Netty server changes on the client, make its can send more than 1 MB of data, the data source is an XML document, the client generally get 10 KB file
Server:
Public class Server {

Public void bind (int port) throws the Exception {
EventLoopGroup bossGroup=new NioEventLoopGroup ();
EventLoopGroup workerGroup=new NioEventLoopGroup ();
Try {
ServerBootstrap ServerBootstrap=new ServerBootstrap ();
ServerBootstrap. Group (bossGroup workerGroup)
Channel (NioServerSocketChannel. Class)
Option (ChannelOption SO_BACKLOG, 10)
ChildHandler (new ChildChannelHander ());
//bind port
ChannelFuture future=serverBootstrap. Bind (port). The sync ();
//wait for the server listening on port closed
Future. Channel (.) closeFuture (). The sync ();
} the finally {
BossGroup. ShutdownGracefully ();
WorkerGroup. ShutdownGracefully ();
}
}

Private class ChildChannelHander extends ChannelInitializer {
@ Override
Protected void initChannel (SocketChannel arg0) {
Try {
Arg0. Pipeline (.) addLast (new StringEncoder (CharsetUtil. UTF_8));
Arg0. Pipeline (.) addLast (new FixedLengthFrameDecoder (10));
Arg0. Pipeline (.) addLast (new StringDecoder (CharsetUtil. UTF_8));
Arg0. Pipeline (.) addLast (new ServerHandler ());
} the catch (Exception e) {
e.printStackTrace();
}
}
}

Public static void main (String [] args) throws the Exception {
Int port=5678;
System. Out.println (" Server: the Status - & gt; Running. ");
The new Server (). The bind (port);
}
}

Service types:
Public class ServerHandler extends ChannelHandlerAdapter {

Private File File;
Private byte [] bytes;
Private FileInputStream FileInputStream;
Private BufferedInputStream BufferedInputStream;
Private static Boolean DEBUG=true;

Public ServerHandler () throws IOException {
The file=new file (" D://1 MB. TXT ");
Bytes=new byte [2047].
FileInputStream=new fileInputStream (file);
BufferedInputStream=new bufferedInputStream (fileInputStream);
}

@ Override
Public void channelRead (ChannelHandlerContext CTX, Object Object) {
Try {
String clientRequest=(String) object;
If (DEBUG)
System. Out.println (" Server: the request - & gt;" + clientRequest);
} the catch (Exception e) {
e.printStackTrace();
}

}

@ Override
Public void channelReadComplete ChannelHandlerContext (CTX) {
If (DEBUG)
System. The out. Println (" Server: Send data ");
Try {
Send (CTX);
} the catch (Exception e) {
e.printStackTrace();
}
}

@ Override
Public void exceptionCaught (ChannelHandlerContext CTX, Throwable cause) {
CTX. Close ();
}

Private void the Send (ChannelHandlerContext CTX) throws IOException {
Int counter=0;
While ((counter=bufferedInputStream. Read (bytes))!=1) {
String String=new String () bytes, 0, counter;
CTX. WriteAndFlush (string);
}
}
}

Client:
Public class Client {

Public void connect (int port, String host) throws the Exception {
EventLoopGroup group=new NioEventLoopGroup ();
Try {
The Bootstrap the Bootstrap=new Bootstrap ();
The bootstrap. Group (group). The channel (NioSocketChannel. Class)
Option (ChannelOption TCP_NODELAY, true)
Handler (new ChannelInitializer () {
@ Override
Public void initChannel SocketChannel (ch) throws the Exception {
Ch. Pipeline (.) addLast (new StringEncoder (CharsetUtil. UTF_8));
Ch. Pipeline (.) addLast (new FixedLengthFrameDecoder (1024));
Ch. Pipeline (.) addLast (new ClientHandler ());
}
});
ChannelFuture future=the bootstrap. Connect (host, port). The sync ();
Future. Channel (.) closeFuture (). The sync ();
} the finally {
Group. ShutdownGracefully ();
}
}
Public static void main (String [] args) throws the Exception {
Int port=5678;
System. Out.println (" the Client: the Status - & gt; Running. ");
The new Client (). The connect (port, "127.0.0.1");
}

}

Service types:
Public class ClientHandler extends ChannelHandlerAdapter {
Private static final Logger Logger=Logger. GetLogger (ClientHandler. Class. GetName ());
Private byte [] MSG.
Private static Boolean DEBUG=true;

Private FileOutputStream FileOutputStream;
Private File File;

Public ClientHandler () throws IOException {
The file=new file (" D://RECV. XML ");
FileOutputStream=new fileOutputStream (file);
}

@ Override
Public void channelActive ChannelHandlerContext (CTX) {
MSG=(" COMMANDSTR). GetBytes ();
ByteBuf buf=Unpooled. Buffer (MSG) length);
Buf. WriteBytes (MSG);
CTX. WriteAndFlush (buf);
//SEND the SEND command
}

@ Override
Public void channelRead (ChannelHandlerContext CTX, Object Object) throws the Exception {
ByteBuf message=(ByteBuf) object;
Byte [] bytes=new byte [message readableBytes ()];
Message. ReadBytes (bytes);

FileOutputStream. Write (bytes);
}

@ Override
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related