Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!postnews.google.com!f16g2000yqk.googlegroups.com!not-for-mail From: Bala Newsgroups: comp.lang.java.programmer Subject: Re: Retrieving Sender Information From A Non-blocking DatagramChannel Date: Mon, 24 Oct 2011 14:42:15 -0700 (PDT) Organization: http://groups.google.com Lines: 130 Message-ID: <0371aeff-32b2-4448-b71b-e91178f4cf38@f16g2000yqk.googlegroups.com> References: <9gm0s4FjsaU1@mid.individual.net> NNTP-Posting-Host: 70.34.79.41 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1319493435 23084 127.0.0.1 (24 Oct 2011 21:57:15 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 24 Oct 2011 21:57:15 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: f16g2000yqk.googlegroups.com; posting-host=70.34.79.41; posting-account=mgP1jQoAAABJDnFmZhkPyIuwwQ_0m5NY User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HUARLSCNK X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9155 On Oct 24, 4:41=A0pm, Robert Klemme wrote: > On 24.10.2011 21:14, Bala wrote: > > > > > > > > > > > I have a DatagramChannel that I have added to a Java Selector. =A0Hence > > the configureblocking is set to false since the selector expects a non > > blockign socket. > > I have to use the DatagramChannel's receive call and cannot use the > > socket.receive which reads the data in a packet due to the nonblocking > > mode. > > > If I would have received / sent DatagramPacket using the > > dc.socket().receive call, then the packet provides you the Sender > > information anyways. > > > The problem is that I need the host/port of the sender from this > > DatagramChannel. =A0I see there are a couple of private variables withi= n > > the DatagramChannel class cachedSenderInetAddress, cachedSenderPort) > > that have this information but those are private variables and I do > > not see any method in the DatagramChannel or its socket to retrieve > > the sender information. > > > Is there a way I can do this? > > > Here is the code snippet for the Receiver > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > > class CReadDatagramChannel { > > > =A0 =A0DatagramChannel dc =3D DatagramChannel.open().configureBlocking(= false); > > > =A0 =A0public CReadDatagramChannel (int port, Selector selector) { > > =A0 =A0 =A0 =A0 =A0 =A0dc.socket().setReuseAddress(true); > > =A0 =A0 =A0 =A0 =A0 =A0dc.socket().bind(new InetSocketAddress(port)); > > =A0 =A0 =A0 =A0 =A0 =A0dc.register(selector, SelectionKey.OP_READ, this= ); > > =A0 =A0} > > > =A0 =A0/* This method gets invoked from the Selector on the isReadable(= ) */ > > =A0 =A0public void processRead () { > > =A0 =A0 =A0 =A0 =A0 =A0// I cannot do dc.socket().receive(packet) since= the blocking is > > false. > > =A0 =A0 =A0 =A0 =A0 =A0ByteBuffer socketBuffer =3D ByteBuffer.allocate(= 1024); > > =A0 =A0 =A0 =A0 =A0 =A0dc.receive (socketBuffer); > > You're almost there: > > "Returns: > The datagram's source address, or null if this channel is in > non-blocking mode and no datagram was immediately available" > > http://download.oracle.com/javase/6/docs/api/java/nio/channels/Datagr...) > > In other words: you just need to change the line into > > final SocketAddress sender =3D dc.receive(socketBuffer); > > and be done. > > > > > > > > > > > =A0 =A0} > > } > > > Here is the code snippet for the Sender > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > > class CWriteDatagramChannel { > > =A0 =A0DatagramChannel dc =3D DatagramChannel.open().configureBlocking(= false); > > > =A0 =A0public CWriteDatagramChannel (int port, Selector selector) { > > =A0 =A0 =A0 =A0 =A0 =A0dc.socket().setReuseAddress(true); > > =A0 =A0 =A0 =A0 =A0 =A0dc.socket().bind(new InetSocketAddress(port)); > > =A0 =A0 =A0 =A0 =A0 =A0dc.register(selector, SelectionKey.OP_READ, this= ); > > =A0 =A0} > > > =A0 =A0/* This method gets invoked from the Selector on the isWritable(= ) */ > > =A0 =A0pubic void processWrite (ByteBuffer writeBuffer, String host, in= t > > port) { > > =A0 =A0 =A0 =A0 =A0 =A0// I cannot do dc.socket().write since the block= ing is false. > > =A0 =A0 =A0 =A0 =A0 =A0dc.send(writeBuffer, new InetSocketAddress (host= , port)); > > =A0 =A0} > > } > > > Is there a way to know the Sender after the receive call in the > > Reader? > > See above. > > Kind regards > > =A0 =A0 =A0 =A0 robert > > -- > remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestp= ractices.com/ Hey Robert, Thanks a ton for that. Really stupid on my part to not have checked the return value of receive :D. Thanks again Bala