UDP Listener
Alex Boga
boga.alexa at gmail.com
Fri Aug 12 02:09:55 EDT 2011
Hi Trustin,
Thanks for the reply, below is the example that works, it's from internet,
and mean-while I found out what it didn't like:
If instead:
DatagramChannel datagramChannel = (DatagramChannel) bootstrap.bind(new
InetSocketAddress(localIpAddress, multicastPort));
I do something like:
DatagramChannel datagramChannel = (DatagramChannel) bootstrap.bind(new
InetSocketAddress(multicastPort));
It works.
I assume that it prefers binding to (lo) instead (eth0), I don't know if
it's because of netty or due to system configuration.
Best regards,
Alex.
-------------------------------------------------------------------------------------------------------------------
import java.net.*; /* import networking package */
import java.io.*; /* import input/output package */
public class mcreceive {
public static final int MAX_LEN = 1024; /* max receive buffer */
public static final int MIN_PORT = 1024; /* min network port */
public static final int MAX_PORT = 65535; /* max network port */
public static void main(String argv[]) {
InetAddress mcAddress=null; /* multicast address */
int mcPort=0; /* multicast port */
int ttl=1; /* time to live */
boolean done=false; /* variable for send loop */
/* validate number of arguments */
if (argv.length != 2) {
System.out.println("Usage: mcreceive " +
"<Multicast IP> <Multicast Port>");
System.exit(1);
}
/* validate the multicast address argument */
try {
mcAddress = InetAddress.getByName(argv[0]); }
catch (UnknownHostException e) {
System.err.println(argv[0] + " is not a valid IP address");
System.exit(1);
}
/* validate address argument is a multicast IP */
if (! mcAddress.isMulticastAddress()) {
System.err.println(mcAddress.getHostAddress() +
" is not a multicast IP address.");
System.exit(1);
}
/* parse and validate port argument */
try {
mcPort = Integer.parseInt(argv[1]);
} catch (NumberFormatException nfe) {
System.out.println("Invalid port number " + argv[1]);
System.exit(1);
}
if ((mcPort < MIN_PORT) || (mcPort > MAX_PORT)) {
System.out.println("Invalid port number " + mcPort);
System.out.println("Port should be in range " + MIN_PORT
+ " to " + MAX_PORT);
System.exit(1);
}
try {
/* instantiate a MulticastSocket */
MulticastSocket sock = new MulticastSocket(mcPort);
/* set the address reuse option */
sock.setReuseAddress(true); // Java 1.4 and higher
/* join the multicast group */
sock.joinGroup(mcAddress);
while (!done) { /* loop forever */
/* create a new DatagramPacket with an empty buffer
*/
byte[] buf = new byte[MAX_LEN];
DatagramPacket packet = new DatagramPacket(buf,
buf.length);
/* wait to receive packet into the DatagramPacket
instance */
sock.receive(packet);
/* output the data from the packet received */
System.out.println("Received " + packet.getLength()
+
" bytes from " + packet.getAddress() + ": "
+ new
String(packet.getData(),0,packet.getLength()));
}
sock.leaveGroup(mcAddress);
sock.close();
} catch (IOException e) {
System.err.println(e.toString());
System.exit(1);
}
}
}
-------------------------------------------------------------------------------------------------------------------
On Fri, Aug 12, 2011 at 6:00 AM, 이희승 (Trustin Lee) <trustin at gmail.com>wrote:
> Could you also post the non-Netty code that works? I also would suggest
> you to remove the decoder from the pipeline just in case of the buggy
> decoder.
>
> --
> Trustin Lee <http://gleamynode.net/>
>
> On Thursday, August 11, 2011 at 10:50 PM, alex_ab_sw wrote:
>
> Hello,
>
> I am trying to write a component in an application that will listen on UDP
> for multicast messages, but somehow I don't receive any messages while with
> a MulticastSocket I can see the messages landing fine.
>
> Here is the piece of code that I am talking about:
> ---------------------------------------------------------------
> DatagramChannelFactory channelFactory = new
> OioDatagramChannelFactory(Executors.newCachedThreadPool());
> ConnectionlessBootstrap bootstrap = new
> ConnectionlessBootstrap(channelFactory);
>
> bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
> @Override
> public ChannelPipeline getPipeline() {
> return Channels.pipeline(messageDecoder, multicastHandler);
> }
> });
>
> bootstrap.setOption("receiveBufferSizePredictorFactory", new
> FixedReceiveBufferSizePredictorFactory(
> maxPacketSize));
>
>
> logger.info("... binding the DatagramChannel on new
> InetSocketAddress {}:{}", localIpAddress, multicastPort);
> DatagramChannel datagramChannel = (DatagramChannel)
> bootstrap.bind(new InetSocketAddress(localIpAddress,
> multicastPort));
>
> try {
> InetAddress multicastAddress =
> InetAddress.getByName(multicastIpAddress);
> datagramChannel.joinGroup(multicastAddress);
> } catch (UnknownHostException e) {
> shutdown();
> }
> ------------------------------------------------------------
> Can you tell if I am doing something wrong, because I see no errors on logs
> but also no messages coming. The component is deployed on a Linux box with
> CentOS on it.
>
> Thanks in advance.
>
> --
> View this message in context:
> http://netty-forums-and-mailing-lists.685743.n2.nabble.com/UDP-Listener-tp6676456p6676456.html
> Sent from the Netty User Group mailing list archive at Nabble.com.
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users
>
>
>
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users
>
--
Have fun & travel light.
Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/netty-users/attachments/20110812/5f17c811/attachment.html
More information about the netty-users
mailing list