जब हैंडलर में 2048bytes लेखन, messageRevieved विधि सभी डेटा प्राप्त करने के लिए दो बार बुलाया जाना चाहिए ... मैं कैसे में, हम केवल 1024bytes से कम डेटा लिख और प्राप्त कर सकते हैं: हम कैसे लिख सकते हैं या प्राप्त कर सकते हैं?
कोड 2048bytes डेटा प्राप्त कर सकते
सर्वर:
public class Server{
public static void main(String[] args){
ChannelFactory factory=new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
ServerBootstrap bootstrap=new ServerBootstrap(factory);
bootstrap.setPipelineFactory(new CarPipelineFactory());
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
bootstrap.bind(new InetSocketAddress(8989));
}
}
सर्वर हैंडलर:
public class ServerHandler extends SimpleChannelHandler{
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e){
byte[] resp=data.getBytes();//data is a String greater than 1024bytes;
ChannelBuffer buffer=ChannelBuffers.buffer(resp.length);
buffer.writerBytes(resp);
e.getChannel().write(buffer);
buffer.clear();
}
}
ग्राहक:
public class Client{
public static void main(String[] args){
ChannelFactory channelFactory=new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
ClientBootstrap bootstrap=new ClientBootstrap(channelFactory);
bootstrap.getPipeline().addLast("handler", new PhoneClientHandler());
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
bootstrap.connect(new InetSocketAddress("127.0.0.1",8181));
}
}
ग्राहक हैंडलर:
public class ClientHandler extends SimpleChannelHandler{
public void messageRecieved(ChannelHandlerContext ctx, ChannelStateEvent e){
ChannelBuffer buffer=(ChannelBuffer)e.getMessage();
int size=buffer.readableBytes();
byte[] bytes=new byte[size];
buffer.readBytes(bytes);
buffer.clear();
System.out.println(new String(bytes));//if the data size>1024,the String will speprate into parts.
}
}
खेद है कि मैं सवाल समझ में नहीं आता। क्या आप अधिक विशिष्ट होने की कोशिश कर सकते हैं? –
मुझे खेद है, मैं एक चीनी हूं और मेरी अंग्रेजी अच्छी नहीं है। नीचे मेरा प्रश्न है: क्योंकि मैं नेटटी का उपयोग कर रहा हूं, जब मैं 2048 बाइट्स (1024bytes से बड़ा) डेटा को एक हाथ से दूसरे में लिखता हूं, तो प्राप्त करने वाले हाथ को दो बार प्राप्त करना चाहिए, मैं एक बार में पूरे डेटा (1024bytes से बड़ा) को कैसे पुनर्जीवित कर सकता हूं ? – Gofier