netty socket最简单的一个示例代码,麻烦看下为什么服务端监听不到消息?解决给分!

码届小王 2020-06-25 11:30:51
如下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();
}
}
}
...全文
2012 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
TowlGol 2021-04-16
  • 打赏
  • 举报
回复
Netty2:粘包/拆包问题与使用 https://zhuanlan.zhihu.com/p/71722002 你看看是不是这个问题呢,所以加一个回车用来拆分包
引用 3 楼 码届小王 的回复:
[quote=引用 1 楼 dmankill 的回复:] 发送的数据问题,你用的编解码器(LengthFieldBasedFrameDecoder)是长度解码器,你发送的数据包没带长度,至于服务器的第一次有响应是连接响应(channelActive)而并不是读取响应(channelRead0)
我服务端改成了LineBasedFrameDecoder pipeline.addLast(new LineBasedFrameDecoder(1024)); 然后客户端每个消息后面都加个“\n”,服务端就监听到了; 不过如第一楼我java原生的代码案例不加“\n”服务端用【while ((len = inputStream.read(data)) != -1) 】也是接受到的,netty版本的服务端想实现这个改怎么办谢谢[/quote]
码届小王 2020-06-26
  • 打赏
  • 举报
回复
引用 1 楼 dmankill 的回复:
发送的数据问题,你用的编解码器(LengthFieldBasedFrameDecoder)是长度解码器,你发送的数据包没带长度,至于服务器的第一次有响应是连接响应(channelActive)而并不是读取响应(channelRead0)

我服务端改成了LineBasedFrameDecoder
pipeline.addLast(new LineBasedFrameDecoder(1024));
然后客户端每个消息后面都加个“\n”,服务端就监听到了;
不过如第一楼我java原生的代码案例不加“\n”服务端用【while ((len = inputStream.read(data)) != -1) 】也是接受到的,netty版本的服务端想实现这个改怎么办谢谢
码届小王 2020-06-26
  • 打赏
  • 举报
回复
引用 1 楼 dmankill 的回复:
发送的数据问题,你用的编解码器(LengthFieldBasedFrameDecoder)是长度解码器,你发送的数据包没带长度,至于服务器的第一次有响应是连接响应(channelActive)而并不是读取响应(channelRead0)

大神,该怎么解决呢,我试了把这两行注释调也不行
// 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方案的服务端该如何配置才能监听到客户端发的消息呢,谢谢!
dmankill 2020-06-26
  • 打赏
  • 举报
回复
发送的数据问题,你用的编解码器(LengthFieldBasedFrameDecoder)是长度解码器,你发送的数据包没带长度,至于服务器的第一次有响应是连接响应(channelActive)而并不是读取响应(channelRead0)
dmankill 2020-06-26
  • 打赏
  • 举报
回复
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeInt((message.length()));
dos.write(message.getBytes());

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧