java - Netty: Using a channel defined in the anonymous inner class within another method -


i have implemented server-client connection in netty, can send , receive data between 2 connections.

    public void start()      {         // start interface         bossgroup = new nioeventloopgroup();         workergroup = new nioeventloopgroup();         try          {             serverbootstrap b = new serverbootstrap();             b.group(bossgroup, workergroup)                  .channel(nioserversocketchannel.class)                  .childhandler(new channelinitializer<socketchannel>() {                      @override                      public void initchannel(socketchannel ch) throws exception {                          log.debug("socket established");                          ch.pipeline().addlast(new simulatorinboundhandler());                          establishedchannel = ch;                           interfaceeventslisteners.announce().interfaceconnected();                           bytebuf buff = unpooled.wrappedbuffer("hello\n\r".getbytes());                          ch.writeandflush(buff);                       }                   })                  .option(channeloption.so_backlog, 128)                  .childoption(channeloption.so_keepalive, true);              // bind , start accept incoming connections.             channelfuture bindfuture = b.bind(simulatorconfiguration.getsimulationport());              bindfuture.await();              senddata();          }         catch (interruptedexception e)          {             log.error("interrupted", e);         }     } 

the child handler creates anonymous class build channel ch

i save channel global variable establishedchannel

in order input live data connection have created method senddata()

public void senddata() {     channel ch = establishedchannel;     ch.pipeline().addlast(new simulatorinboundhandler());     bytebuf buff = unpooled.wrappedbuffer("hi\n\r".getbytes());      if(ch != null)     {         ch.writeandflush(buff);     }     else     {         log.debug("no channel object attached");     }  } 

ch continually identified being null. how can issue?

the problem initchannel(...) called each new channel accepted , calling senddata() after bind future completes makes no sense.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -