java - Netty trouble with chat server -
i'm having trouble figuring out wrong simple netty chat program working with.
i have tried debugging through program hasn't been able show me problem lies.
through debugging seems client connecting server, write function doesn't seem working.
i followed tutorial here. have source code here on github.
here client classes.
public void run() throws exception { eventloopgroup group = new nioeventloopgroup(); try { bootstrap bootstrap = new bootstrap() .group(group) .channel(niosocketchannel.class) .handler(new chatclientinitializer()); channel channel = bootstrap.connect(host, port).sync().channel(); bufferedreader in = new bufferedreader(new inputstreamreader(system.in)); while (true) { channel.write(in.readline() + "\r\n"); } } { group.shutdowngracefully(); } } public class chatclientinitializer extends channelinitializer<socketchannel> { @override protected void initchannel(socketchannel ch) throws exception { channelpipeline pipeline = ch.pipeline(); pipeline.addlast("framer", new delimiterbasedframedecoder(8192,delimiters.linedelimiter())); pipeline.addlast("decoder", new stringdecoder()); pipeline.addlast("encoder", new stringencoder()); pipeline.addlast("handler", new chatclienthandler()); } } public class chatclienthandler extends simplechannelinboundhandler<string>{ @override protected void channelread0(channelhandlercontext ctx, string msg) throws exception { system.out.println(msg); } }
and here server classes
public void run() throws exception{ eventloopgroup bossgroup = new nioeventloopgroup(); eventloopgroup workergroup = new nioeventloopgroup(); try{ serverbootstrap bootstrap = new serverbootstrap() .group(bossgroup,workergroup) .channel(nioserversocketchannel.class) .childhandler(new chatserverinitializer()); bootstrap.bind(port).sync().channel().closefuture().sync(); } { bossgroup.shutdowngracefully(); workergroup.shutdowngracefully(); } } public class chatserverinitializer extends channelinitializer<socketchannel> { @override protected void initchannel(socketchannel ch) throws exception { channelpipeline pipeline = ch.pipeline(); pipeline.addlast("framer", new delimiterbasedframedecoder(8192, delimiters.linedelimiter())); pipeline.addlast("decoder", new stringdecoder()); pipeline.addlast("encoder", new stringencoder()); pipeline.addlast("handler", new chatserverhandler()); } } public class chatserverhandler extends simplechannelinboundhandler<string>{ private static final channelgroup channels = new defaultchannelgroup(globaleventexecutor.instance); @override public void handleradded(channelhandlercontext ctx) throws exception{ channel incoming = ctx.channel(); channels.add(ctx.channel()); for(channel channel : channels){ channel.write("[server] - " + incoming.remoteaddress() + "has joined\n"); } } @override public void handlerremoved(channelhandlercontext ctx) throws exception { channel leaving = ctx.channel(); for(channel channel : channels){ channel.write("[server] - " + leaving.remoteaddress() + "has left\n"); } channels.remove(ctx.channel()); } @override protected void channelread0(channelhandlercontext ctx, string msg) throws exception { channel incoming = ctx.channel(); system.out.println(msg); for(channel channel : channels){ channel.write("[" + incoming.remoteaddress() + "]" + msg +"\n"); } } }
you need replace channel.write(...) channel.writeandflush(...)
Comments
Post a Comment