栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java聊天系统不能互相传送

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java聊天系统不能互相传送

问题描述

client接受到控制台一行数据,用writeandflush发送,服务器端channelread0没有触发。

输入:在控制台输入“123”然后按回车健。

问题出现的环境背景及自己尝试过哪些方法

输入:在控制台输入“123”然后按回车健。

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)

MyServer.java

package com.wss.netty.thirdexample;

import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.ChannelFuture;

import io.netty.channel.EventLoopGroup;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.nio.NioServerSocketChannel;

public class MyServer {

public static void main(String[] args) throws InterruptedException {

EventLoopGroup bossgroup = new NioEventLoopGroup();

EventLoopGroup workergroup = new NioEventLoopGroup();

try {

ServerBootstrap serverBootstrap = new ServerBootstrap();

serverBootstrap.group(bossgroup, workergroup).channel(NioServerSocketChannel.class)

.childHandler(new MyChatServerInitizlization());

ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();

channelFuture.channel().closeFuture().sync();

}finally{

bossgroup.shutdownGracefully();

workergroup.shutdownGracefully();

}

}

}

MyChatServerInitizlization.java

package com.wss.netty.thirdexample;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.ChannelPipeline;

import io.netty.channel.socket.SocketChannel;

import io.netty.handler.codec.DelimiterBasedFrameDecoder;

import io.netty.handler.codec.Delimiters;

import io.netty.handler.codec.string.StringDecoder;

import io.netty.handler.codec.string.StringEncoder;

import io.netty.util.CharsetUtil;

public class MyChatServerInitizlization extends ChannelInitializer {

@Override

protected void initChannel(SocketChannel socketChannel) throws Exception {

ChannelPipeline pipeline = socketChannel.pipeline();

pipeline.addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()));

pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));

pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));

pipeline.addLast(new MyChatServerHandler());

}

}

MyChatServerHandler.java

package com.wss.netty.thirdexample;

import io.netty.channel.Channel;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.SimpleChannelInboundHandler;

import io.netty.channel.group.ChannelGroup;

import io.netty.channel.group.DefaultChannelGroup;

import io.netty.util.concurrent.GlobalEventExecutor;

public class MyChatServerHandler extends SimpleChannelInboundHandler {

private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

@Override

protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {

System.out.println(123);

Channel channel = channelHandlerContext.channel();

System.out.println(123);

System.out.println(s);

channelGroup.forEach(ch -> {

if(channel != ch){

ch.writeAndFlush(ch.remoteAddress() + "发送的消息是" + s);

}else{

ch.writeAndFlush("【自己】:" + s);

}

});

}

@Override

public void handlerAdded(ChannelHandlerContext ctx) throws Exception {

Channel channel = ctx.channel();

channelGroup.writeAndFlush("【服务器】 - " + channel.remoteAddress() + "加入n");

channelGroup.add(channel);

}

@Override

public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {

Channel channel = ctx.channel();

//channelGroup.remove(channel); //netty自动删除

channelGroup.writeAndFlush("【服务器】 - " + channel.remoteAddress() + "断开连接n");

System.out.println(channelGroup.size());

}

@Override

public void channelActive(ChannelHandlerContext ctx) throws Exception {

Channel channel = ctx.channel();

System.out.println(channel.remoteAddress() + "上线");

}

@Override

public void channelInactive(ChannelHandlerContext ctx) throws Exception {

Channel channel = ctx.channel();

System.out.println(channel.remoteAddress() + "下线");

}

@Override

public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

cause.printStackTrace();

ctx.close();

}

}

MyClient.java

package com.wss.netty.thirdexample;

import io.netty.bootstrap.Bootstrap;

import io.netty.channel.Channel;

import io.netty.channel.EventLoopGroup;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.nio.NioSocketChannel;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class MyClient {

public static void main(String[] args) throws InterruptedException, IOException {

EventLoopGroup eventLoopGroup = new NioEventLoopGroup();

try {

Bootstrap bootstrap = new Bootstrap();

bootstrap.group(eventLoopGroup)

.channel(NioSocketChannel.class)

.handler(new MyChatClientInitialization());

Channel channel = bootstrap.connect("127.0.0.1", 8899).sync().channel();

//不断读取控制台输入的程序

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

System.out.println(123);

for(;;){

System.out.println(456);

System.out.println(bufferedReader.readLine()+"1234");

channel.writeAndFlush(bufferedReader.readLine() + "rn");

}

}finally{

eventLoopGroup.shutdownGracefully();

}

}

}

MyChatClientInitialization.java

package com.wss.netty.thirdexample;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.ChannelPipeline;

import io.netty.channel.socket.SocketChannel;

import io.netty.handler.codec.DelimiterBasedFrameDecoder;

import io.netty.handler.codec.Delimiters;

import io.netty.handler.codec.string.StringDecoder;

import io.netty.handler.codec.string.StringEncoder;

import io.netty.util.CharsetUtil;

import java.nio.channels.Channel;

public class MyChatClientInitialization extends ChannelInitializer {

@Override

protected void initChannel(SocketChannel socketChannel) throws Exception {

ChannelPipeline pipeline = socketChannel.pipeline();

pipeline.addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()));

pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));

pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));

pipeline.addLast(new MyChatClientHandler());

}

}

MyChatClientHandler.java

package com.wss.netty.thirdexample;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.SimpleChannelInboundHandler;

public class MyChatClientHandler extends SimpleChannelInboundHandler {

@Override

protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {

System.out.println(s);

}

@Override

public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

cause.printStackTrace();

ctx.close();

}

}

你期待的结果是什么?实际看到的错误信息又是什么?

期待的结果是启动多个客户端,一个客户端发送消息,另一个客户端也能查看到,实现广播的效果。

在控制台输入“123”,然后回车,其他客户端能够查看到消息。

实际的结果是其他客户端没有反映,服务器端channelread0没有被调用。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023824.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号