从服务器netty nio java接收消息
我试图弄清楚如何使用netty nio服务器和从中找到的客户端代码创建聊天。我真正想要的是弄清楚如何准确地使用从客户端和服务器接收到的消息。我在服务器中找到了以下代码,用于键入接收到的消息:从服务器netty nio java接收消息,java,web,server,client,netty,Java,Web,Server,Client,Netty,我试图弄清楚如何使用netty nio服务器和从中找到的客户端代码创建聊天。我真正想要的是弄清楚如何准确地使用从客户端和服务器接收到的消息。我在服务器中找到了以下代码,用于键入接收到的消息: public class EchoClientHandler extends ChannelInboundHandlerAdapter { public String message; @Override public void channelRead(ChannelHandlerCo
public class EchoClientHandler extends ChannelInboundHandlerAdapter {
public String message;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("Got reply: " + msg.toString().trim());
message = msg.toString().trim();
ctx.disconnect();
}
// How to store the message variable and use it from my main function?????
}
此代码代表echoClient:
public class EchoClient {
public String host;
public int port;
public EchoClient(String host, int port) {
this.host = host;
this.port = port;
}
public void send(String msg) throws InterruptedException {
EventLoopGroup eventGroup = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventGroup)
.remoteAddress(host, port)
.channel(NioSocketChannel.class) // TCP server socket
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(
// break stream into "lines"
new LineBasedFrameDecoder(EchoServerHandler.LINE_MAX, false, true),
new StringDecoder(CharsetUtil.UTF_8),
new StringEncoder(CharsetUtil.UTF_8),
new EchoClientHandler()
);
}
});
ChannelFuture f = bootstrap.connect().sync();
System.out.println("Connected!");
f.channel().writeAndFlush(msg).sync();
System.out.print("Sent: " + msg);
f.channel().closeFuture().sync();
eventGroup.shutdownGracefully().sync();
System.out.println("Done.");
}
public static void main(String[] args) throws InterruptedException {
EchoClientHandler temp = new EchoClientHandler(); //how can i have access to this variable and the returned message?
String host ="127.0.0.1";
int port = 8080;
EchoClient client = new EchoClient(host, port);
client.send("Hello world!\n");
temp.message?
}
}
公共类EchoClient{
公共字符串主机;
公共国际港口;
公共EchoClient(字符串主机,int端口){
this.host=host;
this.port=端口;
}
public void send(字符串msg)抛出InterruptedException{
EventLoopGroup eventGroup=新的NioEventLoopGroup();
Bootstrap Bootstrap=新的Bootstrap();
bootstrap.group(事件组)
.remoteAddress(主机、端口)
.channel(NioSocketChannel.class)//TCP服务器套接字
.handler(新的通道初始值设定项(){
@凌驾
受保护的无效初始化通道(SocketChannel SocketChannel)引发异常{
socketChannel.pipeline().addLast(
//将流分成“行”
新的LineBasedFrameDecoder(EchoServerHandler.LINE_MAX,false,true),
新的字符串解码器(CharsetUtil.UTF_8),
新的StringEncoder(CharsetUtil.UTF_8),
新EchoClientHandler()
);
}
});
ChannelFuture f=bootstrap.connect().sync();
System.out.println(“已连接!”);
f、 channel().writeAndFlush(msg.sync();
系统输出打印(“发送:+msg”);
f、 通道().closeFuture().sync();
eventGroup.shutdownGracefully().sync();
System.out.println(“完成”);
}
公共静态void main(字符串[]args)引发InterruptedException{
EchoClientHandler temp=new EchoClientHandler();//如何访问此变量和返回的消息?
字符串host=“127.0.0.1”;
int端口=8080;
EchoClient客户端=新的EchoClient(主机、端口);
client.send(“Hello world!\n”);
临时消息?
}
}
在这里的代码中有一条打印消息,但是对象msg如何与接收到的消息连接?我想用的时候怎么用
编辑:
频道中的公共字符串消息
有一个值,但在主消息中为空。如何正确传递值?猜测EchoCLientHandler和EchoClient与EchoClient中的主函数是不同的线程。但是,有没有办法从我的主功能读取从EchoClientHandler收到的消息?您使用StringEncoder
对传入的ByteBuf
缓冲区进行编码,StringDecoder
对传出的消息进行解码。您可能需要像处理bgTaskGroup.submit(newsleep(sleepMillis,ctx.channel())一样处理它代码>在BGTaskServerHandler
handler.对不起,我真的没听懂你的话。我想从我的客户端的主要功能中读取消息。连接完成后,如何打印来自main而非channelRead的消息,或者在新变量中解析消息。请您详细说明一下?更新问题,包括整个代码表单客户端。channelRead
和main
方法在不同的线程中(异步io)。您可以使用中间数据类型来存储和检索EchoClientHandler
类。我得到了main和channelRead来自不同线程的部分,但是如何将值存储在中间数据类型中?你能提供一个例子吗?另外,我不想从EchoClientHandler中检索信息,而是从我的主函数中检索信息。