Ok.  I&#39;ll let me ask the question a little differently - &quot;How do I send data to the client continuously?&quot;.  <br><br><br>In the code below I&#39;m trying to send localtime every 5 secs to the client from within channelConnected method.<br>
But it doesn&#39;t work for me.  It works only if I comment the while loop.<br>So if I want to send client time every 5 secs how do i do it?<br><br>        public class SRNGServer {<br><br>            public static void main(String[] args) throws Exception {<br>
                // Configure the server.<br>                ServerBootstrap bootstrap = new ServerBootstrap(<br>                        new NioServerSocketChannelFactory(<br>                                Executors.newCachedThreadPool(),<br>
                                Executors.newCachedThreadPool()));<br><br>                // Configure the pipeline factory.<br>                bootstrap.setPipelineFactory(new SRNGServerPipelineFactory());<br><br>                // Bind and start to accept incoming connections.<br>
                bootstrap.bind(new InetSocketAddress(8080));<br>            }<br>        }<br><br><br><br>        public class SRNGServerHandler extends SimpleChannelUpstreamHandler {<br><br>            private static final Logger logger = Logger.getLogger(<br>
                    SRNGServerHandler.class.getName());<br><br><br>            @Override<br>            public void channelConnected(<br>                    ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {<br>
                // Send greeting for a new connection.<br>              ChannelFuture writeFuture=e.getChannel().write(&quot;Welcome to &quot; + InetAddress.getLocalHost().getHostName() + &quot;!\r\n&quot;);<br><br><br>              while(true){<br>
                e.getChannel().write(&quot;It is &quot; + new Date() + &quot; now.\r\n&quot;);<br><br>                Thread.sleep(1000*5);<br>              }<br>            }<br><br>            @Override<br>            public void exceptionCaught(<br>
                    ChannelHandlerContext ctx, ExceptionEvent e) {<br>                logger.log(<br>                        Level.WARNING,<br>                        &quot;Unexpected exception from downstream.&quot;,<br>
                        e.getCause());<br>                e.getChannel().close();<br>            }<br>        }<br><br><br><br>        public class SRNGServerPipelineFactory implements<br>                ChannelPipelineFactory {<br>
<br>            public ChannelPipeline getPipeline() throws Exception {<br>                // Create a default pipeline implementation.<br>                ChannelPipeline pipeline = pipeline();<br><br>                pipeline.addLast(&quot;encoder&quot;, new StringEncoder());<br>
                pipeline.addLast(&quot;handler&quot;, new SRNGServerHandler());<br><br>                return pipeline;<br>            }<br>        }<br><br><br><div class="gmail_quote">On Fri, Jul 22, 2011 at 2:07 AM, Ioan Eugen Stan <span dir="ltr">&lt;<a href="mailto:stan.ieugen@gmail.com">stan.ieugen@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">2011/7/22 Amit Phatarphekar &lt;<a href="mailto:amitechforums@gmail.com">amitechforums@gmail.com</a>&gt;:<br>

<div><div></div><div class="h5">&gt; I&#39;m planning to use Netty to design a TCP Server.  When the client connects,<br>
&gt; I have to immediately start pumping<br>
&gt; XML data to the client continuously...for hours/days.  Its that simple.<br>
&gt;<br>
&gt; So, I override &quot;channelConnected&quot; method and send data from that method,<br>
&gt; right?...thats great.<br>
&gt;<br>
&gt;<br>
&gt; I will be using the following ChannelFactory<br>
&gt;         ChannelFactory factory =<br>
&gt;             new NioServerSocketChannelFactory(<br>
&gt;                     Executors.newCachedThreadPool(),<br>
&gt;                     Executors.newCachedThreadPool());<br>
&gt;<br>
&gt; NioServerSocketChannelFactory documentation says -&gt; &quot;A worker thread<br>
&gt; performs non-blocking read and write for one or more Channels in a<br>
&gt; non-blocking mode.&quot;  Good.<br>
&gt;<br>
&gt; According to effective Java &quot;Item 51: Don&#39;t depend on the thread scheduler&quot;,<br>
&gt; I want the worker thread to do a &quot;unit of work&quot; and then finish/return.<br>
&gt;<br>
&gt; So in my case, though I have to send data continuously, I want to send some<br>
&gt; chunk (lets say 1 MB ) and then be done (unit of work completed), so that<br>
&gt; worker thread can return.  Then I&#39;ll send another 1 MB.<br>
&gt;<br>
&gt;<br>
&gt; Below example is from the official guide of Netty here -&gt;<br>
&gt; <a href="http://docs.jboss.org/netty/3.2/guide/html_single/index.html#d0e440" target="_blank">http://docs.jboss.org/netty/3.2/guide/html_single/index.html#d0e440</a><br>
&gt;<br>
&gt; I guess the question is then, in this scenario, if i had to unconditionally<br>
&gt; keep sending time to the client, how would I do it, considering<br>
&gt; each send as a unit of work.<br>
&gt;<br>
&gt;<br>
&gt; One way of doing it would be to just put a while loop and do a<br>
&gt; Thread.Sleep.  Any other way?<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;         package org.jboss.netty.example.time;<br>
&gt;<br>
&gt;         public class TimeServerHandler extends SimpleChannelHandler {<br>
&gt;<br>
&gt;             @Override<br>
&gt;             public void channelConnected(ChannelHandlerContext ctx,<br>
&gt; ChannelStateEvent e) {1<br>
&gt;                 Channel ch = e.getChannel();<br>
&gt;<br>
&gt;                 ChannelBuffer time = ChannelBuffers.buffer(4);<br>
&gt;                 time.writeInt(System.currentTimeMillis() / 1000);<br>
&gt;<br>
&gt;                 ChannelFuture f = ch.write(time);<br>
&gt;<br>
&gt;                 f.addListener(new ChannelFutureListener() {<br>
&gt;                     public void operationComplete(ChannelFuture future) {<br>
&gt;                         Channel ch = future.getChannel();<br>
&gt;                         ch.close();<br>
&gt;                     }<br>
&gt;                 });<br>
&gt;             }<br>
&gt;<br>
&gt;             @Override<br>
&gt;             public void exceptionCaught(ChannelHandlerContext ctx,<br>
&gt; ExceptionEvent e) {<br>
&gt;                 e.getCause().printStackTrace();<br>
&gt;                 e.getChannel().close();<br>
&gt;             }<br>
&gt;         }<br>
&gt;<br>
&gt;<br>
</div></div>&gt; _______________________________________________<br>
&gt; netty-users mailing list<br>
&gt; <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
&gt;<br>
<br>
I didn&#39;t understand what exactly is the question, but from what I know<br>
you should have no problems sending data continuously for long periods<br>
of time. And the amount you mentioned is not that large if we are<br>
talking about Ethernet speeds. You should give it a try and see what<br>
happens. If you run into trouble, seek help.<br>
<br>
I don&#39;t think the sleep is necessary. Reading and writing in Netty is<br>
done when there is data available and when it&#39;s room in the buffer.<br>
<br>
Hope that helps,<br>
<font color="#888888">--<br>
Ioan Eugen Stan<br>
<a href="http://ieugen.blogspot.com/" target="_blank">http://ieugen.blogspot.com/</a><br>
<br>
_______________________________________________<br>
netty-users mailing list<br>
<a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a></font></blockquote></div><br>