如下socket服務器端是用netty實作的最簡單的例子,
socket客戶端是用原生java寫的;
客戶端建立連接后MyServerHandler 的channelActive能監聽到,但為什么channelRead0方法一直監聽不到資料?
請大神幫忙看下哪里問題,解決給分!
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
/**
* @author
*/
public final class ServerSimplest {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workGroup).channel(NioServerSocketChannel.class).
childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel socketChannel) {
System.out.println("initChannel:"+socketChannel);
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,4,0,4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new MyServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(8888).sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e){
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
}
class MyServerHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
System.out.println("服務端收到"+ ctx.channel().remoteAddress()+","+s);
ctx.writeAndFlush("服務端收到"+s);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("來自服務端的問候");
ctx.writeAndFlush("來自服務端的問候 \n");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
java原生客戶端(沒參考netty)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client {
private static final String HOST = "127.0.0.1";
private static final int PORT = 8888;
public static void main(String[] args) throws IOException {
final Socket socket = new Socket(HOST, PORT);
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("客戶端啟動成功!");
while (true) {
try {
String message = "hello world";
System.out.println("客戶端發送資料: " + message);
socket.getOutputStream().write(message.getBytes());
} catch (Exception e) {
System.out.println("寫資料出錯!");
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
//接收服務器端的回應
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String info =null;
while ((info = br.readLine())!=null){
System.out.println("接收到了服務端的回應!" + info);
}
}
}
運行后
服務端列印:
initChannel:[id: 0xd0305a18, L:/127.0.0.1:8888 - R:/127.0.0.1:12462]
來自服務端的問候
客戶端列印:
客戶端啟動成功!
客戶端發送資料: hello world
接收到了服務端的回應! 來自服務端的問候
客戶端發送資料: hello world
客戶端發送資料: hello world
客戶端發送資料: hello world
客戶端發送資料: hello world
而如果我用這個java原生服務端(沒參考netty)是能監聽到helloworld的
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerBoot {
private static final int PORT = 8888;
public static void main(String[] args) {
Server server = new Server(PORT);
server.start();
}
}
class Server {
private ServerSocket serverSocket;
public Server(int port) {
try {
this.serverSocket = new ServerSocket(port);
System.out.println("服務端啟動成功,埠:" + port);
} catch (IOException exception) {
System.out.println("服務端啟動失敗");
}
}
public void start() {
new Thread(new Runnable() {
@Override
public void run() {
doStart();
}
}).start();
}
private void doStart() {
while (true) {
try {
Socket client = serverSocket.accept();
new ClientHandler(client).start();
} catch (IOException e) {
System.out.println("服務端例外");
}
}
}
}
class ClientHandler {
public static final int MAX_DATA_LEN = 1024;
private final Socket socket;
public ClientHandler(Socket socket) {
this.socket = socket;
}
public void start() {
System.out.println("新客戶端接入");
new Thread(new Runnable() {
@Override
public void run() {
doStart();
}
}).start();
}
private void doStart() {
try {
InputStream inputStream = socket.getInputStream();
// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
PrintWriter bw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
while (true) {
byte[] data = new byte[MAX_DATA_LEN];
int len;
while ((len = inputStream.read(data)) != -1) {
String message = new String(data, 0, len);
System.out.println("客戶端傳來訊息: " + message);
// socket.getOutputStream().write(data);
// socket.getOutputStream().flush();
// bw.write("服務端回傳的資訊: "+message);//可能得加\n
bw.println("服務端回傳的資訊: "+message);
bw.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
uj5u.com熱心網友回復:
發送的資料問題,你用的編解碼器(LengthFieldBasedFrameDecoder)是長度解碼器,你發送的資料包沒帶長度,至于服務器的第一次有回應是連接回應(channelActive)而并不是讀取回應(channelRead0)uj5u.com熱心網友回復:
大神,該怎么解決呢,我試了把這兩行注釋調也不行
// pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,4,0,4));
// pipeline.addLast(new LengthFieldPrepender(4));
最好能修改服務器端代碼去適應客戶端請求的方式,我java原生服務端接收的方式應該是第81行代碼【while ((len = inputStream.read(data)) != -1) 】這樣去判斷的,可以監聽到客戶端發的每條訊息,netty方案的服務端該如何配置才能監聽到客戶端發的訊息呢,謝謝!
uj5u.com熱心網友回復:
我服務端改成了LineBasedFrameDecoder
pipeline.addLast(new LineBasedFrameDecoder(1024));
然后客戶端每個訊息后面都加個“\n”,服務端就監聽到了;
不過如第一樓我java原生的代碼案例不加“\n”服務端用【while ((len = inputStream.read(data)) != -1) 】也是接受到的,netty版本的服務端想實作這個改怎么辦謝謝
uj5u.com熱心網友回復:
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeInt((message.length()));
dos.write(message.getBytes());
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/46100.html
標籤:Java相關
