java - Simple netty echo server not allowing multiple clients to connect simultaneously -
edit: removed client code because problem appears on server.
i implemented simple netty echo server similar suggested in tutorial.
the problem when attempt run multiple clients pointing same server, seems server allows 1 connection @ time. example, when run "telnet localhost 8080" , try use echo server 2 different clients, first 1 succeeds while second 1 fails (doesn't reply back). also, when start multiple netty clients, see on server side messages being received 1 client port -- when client finishes , closes connection, second client proceeds. know why might happening? using netty 3.5.1. worth noting, netstat running on machine though there isn't lot of output. here's source code server (problem seems on server since can reproduce problem telnet):
server:
import java.net.inetsocketaddress; import java.util.concurrent.executors; import java.util.concurrent.atomic.atomicinteger; import org.jboss.netty.bootstrap.serverbootstrap; import org.jboss.netty.channel.channel; import org.jboss.netty.channel.channelfactory; import org.jboss.netty.channel.channelhandlercontext; import org.jboss.netty.channel.channelpipeline; import org.jboss.netty.channel.channelpipelinefactory; import org.jboss.netty.channel.channels; import org.jboss.netty.channel.exceptionevent; import org.jboss.netty.channel.messageevent; import org.jboss.netty.channel.simplechannelhandler; import org.jboss.netty.channel.socket.nio.nioserversocketchannelfactory; public class server { public static void main(string[] args) { channelfactory factory = new nioserversocketchannelfactory( executors.newsinglethreadexecutor(), executors.newsinglethreadexecutor()); serverbootstrap bootstrap = new serverbootstrap(factory); bootstrap.setpipelinefactory(new channelpipelinefactory() { public channelpipeline getpipeline() { return channels.pipeline(new echoserverhandler()); } }); bootstrap.setoption("child.tcpnodelay", true); bootstrap.setoption("child.keepalive", true); bootstrap.bind(new inetsocketaddress(8080)); } private static atomicinteger count = new atomicinteger(0); public static class echoserverhandler extends simplechannelhandler { @override public void messagereceived(channelhandlercontext ctx, messageevent e) { channel ch = e.getchannel(); int t = count.incrementandget(); if (t % 1000 == 0) { system.out.println("server " + t + " " + e.getchannel()); } ch.write(e.getmessage()); } @override public void exceptioncaught(channelhandlercontext ctx, exceptionevent e) { e.getcause().printstacktrace(); channel ch = e.getchannel(); ch.close(); } } } update: appears can run many clients have worker threads on server (from experimentation different numbers of clients , different fixed worker thread pool sizes). here jstack 1 of worker threads, stuck on epollwait (anyone know why?):
"new i/o worker #1" prio=10 tid=0x00007fdba801a000 nid=0x2b84 runnable [0x00007fdc296c2000] java.lang.thread.state: runnable @ sun.nio.ch.epollarraywrapper.epollwait(native method) @ sun.nio.ch.epollarraywrapper.poll(epollarraywrapper.java:228) @ sun.nio.ch.epollselectorimpl.doselect(epollselectorimpl.java:81) @ sun.nio.ch.selectorimpl.lockanddoselect(selectorimpl.java:87) - locked <0x00000006b11732b0> (a sun.nio.ch.util$2) - locked <0x00000006b11732a0> (a java.util.collections$unmodifiableset) - locked <0x00000006b1173080> (a sun.nio.ch.epollselectorimpl) @ sun.nio.ch.selectorimpl.select(selectorimpl.java:98) @ org.jboss.netty.channel.socket.nio.selectorutil.select(selectorutil.java:52) @ org.jboss.netty.channel.socket.nio.abstractnioworker.run(abstractnioworker.java:209) @ org.jboss.netty.channel.socket.nio.nioworker.run(nioworker.java:35) @ org.jboss.netty.util.threadrenamingrunnable.run(threadrenamingrunnable.java:102) @ org.jboss.netty.util.internal.deadlockproofworker$1.run(deadlockproofworker.java:42) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ java.lang.thread.run(thread.java:724)
Comments
Post a Comment