Java Forum / General / December 2005
Need help with Java MulticastSocket
rick - 03 Dec 2005 16:55 GMT I'm trying to use Java multicasting on a single computer (Linux) for testing purposes and can't get it to work. I'm using the example from the Java Tutorial: http://java.sun.com/docs/books/tutorial/networking/datagrams/broadcasting.html.
The relevant code is as follows (modified a bit here to make it simpler):
Server:
String testMsg = "TestMessage"; byte[] buf = testMsg.getBytes(); InetAddress group = InetAddress.getByName("230.0.0.1"); DatagramPacket packet; packet = new DatagramPacket(buf, buf.length, group, 4446); socket.send(packet); System.out.println("Test message sent");
Client:
MulticastSocket socket; socket = new MulticastSocket(4446); InetAddress address = InetAddress.getByName("230.0.0.1"); socket.joinGroup(address); DatagramPacket packet; byte[] buf = new byte[256]; packet = new DatagramPacket(buf, buf.length); socket.receive(packet); String received = new String(packet.getData()); System.out.println("Message received: " + received);
I start the client first and it stops at "socket.receive(packet), which I verify in a debugger. Then I start the server, which successfully sends the DatagramPacket. No exceptions are thrown by either the client or server. If I run "netstat -gn", I can see the 230.0.0.1 multicast group is created when the client calls socket.joinGroup(). The problem is that the client never receives the datagram from the server.
Help. I can't figure out what's going wrong here.
Thanks,
Rick
Knute Johnson - 03 Dec 2005 17:58 GMT > I'm trying to use Java multicasting on a single computer (Linux) for > testing purposes and can't get it to work. I'm using the example from [quoted text clipped - 38 lines] > > Rick Rick:
Are you running IPTABLES? Could that have a chain that is blocking it?
 Signature Knute Johnson email s/nospam/knute/
rick - 03 Dec 2005 21:05 GMT >> I'm trying to use Java multicasting on a single computer (Linux) for >> testing purposes and can't get it to work. I'm using the example from [quoted text clipped - 42 lines] > > Are you running IPTABLES? Could that have a chain that is blocking it? I don't think so. I did a "ps -efw |grep iptables" and nothing found. I did an "iptables --list -v", but I'm not sure what to look for in the listing.
Thanks.
Knute Johnson - 04 Dec 2005 18:24 GMT >>>I'm trying to use Java multicasting on a single computer (Linux) for >>>testing purposes and can't get it to work. I'm using the example from [quoted text clipped - 48 lines] > > Thanks. I don't see anything wrong, why don't you post a compilable test that doesn't work so we can try it.
 Signature Knute Johnson email s/nospam/knute/
rick - 04 Dec 2005 20:41 GMT >>>>I'm trying to use Java multicasting on a single computer (Linux) for >>>>testing purposes and can't get it to work. I'm using the example from [quoted text clipped - 51 lines] > I don't see anything wrong, why don't you post a compilable test that > doesn't work so we can try it. Hmmm. Me neither. One fact that I failed to mention is that I had this code working a year ago on the same machine. I'm working on a project that I put away and have resurrected and have found this feature not working anymore.
The code can be obtained from the Java Tutorial website. Here are links to the tutorial page and the files to download:
The tutorial:
http://java.sun.com/docs/books/tutorial/networking/datagrams/broadcasting.html
Files:
http://java.sun.com/docs/books/tutorial/networking/datagrams/example-1dot1/Multi castServer.java http://java.sun.com/docs/books/tutorial/networking/datagrams/example-1dot1/Multi castServerThread.java http://java.sun.com/docs/books/tutorial/networking/datagrams/example-1dot1/Multi castClient.java http://java.sun.com/docs/books/tutorial/networking/datagrams/example-1dot1/Quote ServerThread.java http://java.sun.com/docs/books/tutorial/networking/datagrams/example-1dot1/one-l iners.txt
Instructions:
First run MulticastClient.class, which does a MulticastSocket.joinGroup(). Then run MulticastServer.class, which sends DatagramPacketS to the multicast group, which the client is supposed to receive and print to standard out.
I'm running Java 1.5.0 build 1.5.0-b64 on Linux 2.6 kernel. I tried running this from the command line and from Eclipse. Same result.
Thanks very much for your efforts.
Knute Johnson - 05 Dec 2005 01:52 GMT Try this and let me know if it works.
import java.io.*; import java.net.*;
public class mtest { public mtest() { Runnable server = new Runnable() { public void run() { try { int n = 0; InetAddress address = InetAddress.getByName("230.0.0.1"); DatagramSocket ds = new DatagramSocket(); while (true) { byte[] buf = Integer.toString(n++).getBytes(); DatagramPacket dp = new DatagramPacket(buf,buf.length, address,12345); ds.send(dp); try { Thread.sleep(1000); } catch (InterruptedException ie) { } } } catch (IOException ioe) { ioe.printStackTrace(); System.exit(0); } } };
Runnable client = new Runnable() { public void run() { try { InetAddress group = InetAddress.getByName("230.0.0.1"); MulticastSocket ms = new MulticastSocket(12345); ms.joinGroup(group); while (true) { byte[] buf = new byte[32]; DatagramPacket dp = new DatagramPacket(buf,buf.length); ms.receive(dp); String str = new String(dp.getData(),dp.getOffset(), dp.getLength()); System.out.println(str); } } catch (IOException ioe) { ioe.printStackTrace(); System.exit(0); } } };
new Thread(client).start(); new Thread(server).start(); }
public static void main(String[] args) { new mtest(); } }
 Signature Knute Johnson email s/nospam/knute/
rick - 05 Dec 2005 13:36 GMT That doesn't work either. Same result. The sender sends, but nothing is ever received...very strange. I assume that the example from the Java Tutorial worked for you?
Rick
Knute Johnson - 05 Dec 2005 16:42 GMT > That doesn't work either. Same result. The sender sends, but nothing > is ever received...very strange. I assume that the example from > the Java Tutorial worked for you? > > Rick Rick:
I just wrote that one to see if it would work. It works fine on XP but didn't work on Linux until I opened a port in the firewall. I'm pretty sure that's where your problem is. You didn't say which version of Linux you are running but in FC they use iptables.
 Signature Knute Johnson email s/nospam/knute/
rick - 06 Dec 2005 12:15 GMT Knute,
You're a genius! Ok, here's what I did. I poked around my system and found a program called Guarddog, which manages iptables via a very nice GUI. As a simple test, I temporarily disabled the firewall completely and then ran your multicast program. Well, obviously, it worked. I still have a question. Can you give me some guidance on what setting in the firewall is likely causing my problem here. Clearly, I don't want to disable the entire firewall. Also, do you know if most Linux systems are likely to be configured with this setting restricted, preventing multicast from working?
Thanks again.
Rick
rick - 06 Dec 2005 17:43 GMT Assuming that multicasting uses UDP (is that correct?) I added a UDP port matching the port used in our multicasting test program via Guarddog. Guarddog indicated that iptables was successfully updated, but when I do an "iptables -L" to get a listing, the UDP port I added is not included in the listing. And the multicast test still doesn't work. Do you know what I'm missing here?
Thanks,
Rick
Knute Johnson - 07 Dec 2005 05:12 GMT > Assuming that multicasting uses UDP (is that correct?) I added a UDP port > matching the port used in our multicasting test program via Guarddog. [quoted text clipped - 6 lines] > > Rick I'm not sure. Try an iptables -L and see if there is a chain with the UDP port in it. And post the output of iptables -L here too.
 Signature Knute Johnson email s/nospam/knute/
rick - 07 Dec 2005 14:29 GMT >> Assuming that multicasting uses UDP (is that correct?) I added a UDP port >> matching the port used in our multicasting test program via Guarddog. [quoted text clipped - 9 lines] > I'm not sure. Try an iptables -L and see if there is a chain with the > UDP port in it. And post the output of iptables -L here too. I had done an iptables -L (see above). I tried again with Guarddog (added 12345 to match your program) and it doesn't show up. Here's the output: (btw, I really appreciate your help so far. If you don't know why Guarddog isn't working, perhaps I should take this question to a more appropriate forum rather than taking more of your time. If so, just let me know.)
root@1[scripts]# iptables -L Chain INPUT (policy DROP) target prot opt source destination ACCEPT all -- anywhere anywhere ACCEPT all -- 192.168.1.101 192.168.1.255 logaborted tcp -- anywhere anywhere state RELATED,ESTABLISHED tcp flags:RST/RST ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT icmp -- anywhere anywhere icmp destination-unreachable ACCEPT icmp -- anywhere anywhere icmp time-exceeded ACCEPT icmp -- anywhere anywhere icmp parameter-problem nicfilt all -- anywhere anywhere srcfilt all -- anywhere anywhere
Chain FORWARD (policy DROP) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT icmp -- anywhere anywhere icmp destination-unreachable ACCEPT icmp -- anywhere anywhere icmp time-exceeded ACCEPT icmp -- anywhere anywhere icmp parameter-problem srcfilt all -- anywhere anywhere
Chain OUTPUT (policy DROP) target prot opt source destination ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT icmp -- anywhere anywhere icmp destination-unreachable ACCEPT icmp -- anywhere anywhere icmp time-exceeded ACCEPT icmp -- anywhere anywhere icmp parameter-problem s1 all -- anywhere anywhere
Chain f0to1 (3 references) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpt:ipp state NEW ACCEPT udp -- anywhere anywhere udp dpt:ipp ACCEPT tcp -- anywhere anywhere tcp dpt:netbios-ns state NEW ACCEPT udp -- anywhere anywhere udp spts:1024:65535 dpt:netbios-ns ACCEPT udp -- anywhere anywhere udp spt:netbios-ns dpt:netbios-ns ACCEPT udp -- anywhere anywhere udp spts:1024:65535 dpt:netbios-dgm ACCEPT udp -- anywhere anywhere udp spt:netbios-dgm dpt:netbios-dgm ACCEPT tcp -- anywhere anywhere tcp dpt:netbios-ssn state NEW ACCEPT udp -- anywhere anywhere udp spts:1024:65535 dpt:netbios-ssn ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpt:6969 state NEW ACCEPT icmp -- anywhere anywhere icmp source-quench ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpt:www state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpt:webcache state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpt:8008 state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpt:8000 state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpt:8888 state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:microsoft-ds state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpts:6881:6889 state NEW ACCEPT udp -- anywhere anywhere udp spt:netbios-ns dpts:1024:5999 ACCEPT udp -- anywhere anywhere udp spt:netbios-ns dpt:netbios-ns ACCEPT udp -- anywhere anywhere udp spt:netbios-dgm dpt:netbios-dgm ACCEPT icmp -- anywhere anywhere icmp echo-reply ACCEPT udp -- anywhere anywhere udp dpts:6970:7170 ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpts:6881:6889 state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpts:1024:65535 state NEW logdrop all -- anywhere anywhere
Chain f1to0 (1 references) target prot opt source destination ACCEPT udp -- anywhere anywhere udp spt:netbios-ns dpts:1024:65535 ACCEPT udp -- anywhere anywhere udp spt:netbios-ns dpt:netbios-ns ACCEPT udp -- anywhere anywhere udp spt:netbios-dgm dpt:netbios-dgm ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpts:6881:6889 state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:ipp state NEW ACCEPT udp -- anywhere anywhere udp dpt:ipp ACCEPT tcp -- anywhere anywhere tcp dpt:domain state NEW ACCEPT udp -- anywhere anywhere udp dpt:domain ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:smtp state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:netbios-ns state NEW ACCEPT udp -- anywhere anywhere udp spts:1024:5999 dpt:netbios-ns ACCEPT udp -- anywhere anywhere udp spt:netbios-ns dpt:netbios-ns ACCEPT udp -- anywhere anywhere udp spts:1024:5999 dpt:netbios-dgm ACCEPT udp -- anywhere anywhere udp spt:netbios-dgm dpt:netbios-dgm ACCEPT tcp -- anywhere anywhere tcp dpt:netbios-ssn state NEW ACCEPT udp -- anywhere anywhere udp spts:1024:5999 dpt:netbios-ssn ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:1723 state NEW ACCEPT gre -- anywhere anywhere ACCEPT udp -- anywhere anywhere udp spts:1024:5999 dpt:time ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:time state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:kerberos state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:whois state NEW ACCEPT udp -- anywhere anywhere udp dpt:43 ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpts:6660:6669 state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:ftp state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:https state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:ldap state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:522 state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:1503 state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:1720 state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:1731 state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpts:1024:65535 state NEW ACCEPT udp -- anywhere anywhere udp spts:1024:5999 dpts:1024:65535 ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:5050 state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:telnet state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpts:5000:5001 state NEW ACCEPT udp -- anywhere anywhere udp spts:1024:5999 dpt:5000 ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:6969 state NEW ACCEPT udp -- anywhere anywhere udp dpt:ntp ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:pop3 state NEW ACCEPT udp -- anywhere anywhere udp dpts:5060:5061 ACCEPT icmp -- anywhere anywhere icmp echo-request ACCEPT tcp -- anywhere anywhere tcp dpt:554 state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:7070 state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:xmpp-client state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpts:5190:5193 state NEW ACCEPT udp -- anywhere anywhere udp spts:1024:5999 dpts:5190:5193 ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:imaps state NEW ACCEPT udp -- anywhere anywhere udp dpt:3478 ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:ssh state NEW ACCEPT tcp -- anywhere anywhere tcp spts:0:1023 dpt:ssh state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:3030 state NEW ACCEPT icmp -- anywhere anywhere icmp source-quench ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:pop3s state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:dict state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:8765 state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:nntp state NEW ACCEPT udp -- anywhere anywhere udp dpts:33434:33600 ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:www state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:webcache state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:8008 state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:8000 state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:8888 state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:printer state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:microsoft-ds state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpts:6881:6889 state NEW ACCEPT udp -- anywhere anywhere udp dpt:4000 ACCEPT tcp -- anywhere anywhere tcp spts:1024:65535 dpts:1024:65535 state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:imap2 state NEW ACCEPT udp -- anywhere anywhere udp dpt:imap2 ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:rsync state NEW ACCEPT tcp -- anywhere anywhere tcp dpt:gnutella-svc state NEW ACCEPT tcp -- anywhere anywhere tcp spts:1024:5999 dpt:1863 state NEW logdrop all -- anywhere anywhere
Chain logaborted (1 references) target prot opt source destination logaborted2 all -- anywhere anywhere limit: avg 1/sec burst 10 LOG all -- anywhere anywhere limit: avg 2/min burst 1 LOG level warning prefix `LIMITED '
Chain logaborted2 (1 references) target prot opt source destination LOG all -- anywhere anywhere LOG level warning tcp-sequence tcp-options ip-options prefix `ABORTED ' ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
Chain logdrop (4 references) target prot opt source destination logdrop2 all -- anywhere anywhere limit: avg 1/sec burst 10 LOG all -- anywhere anywhere limit: avg 2/min burst 1 LOG level warning prefix `LIMITED ' DROP all -- anywhere anywhere
Chain logdrop2 (1 references) target prot opt source destination LOG all -- anywhere anywhere LOG level warning tcp-sequence tcp-options ip-options prefix `DROPPED ' DROP all -- anywhere anywhere
Chain logreject (0 references) target prot opt source destination logreject2 all -- anywhere anywhere
Chain logreject2 (1 references) target prot opt source destination REJECT tcp -- anywhere anywhere reject-with tcp-reset REJECT udp -- anywhere anywhere reject-with icmp-port-unreachable DROP all -- anywhere anywhere
Chain nicfilt (1 references) target prot opt source destination RETURN all -- anywhere anywhere RETURN all -- anywhere anywhere RETURN all -- anywhere anywhere logdrop all -- anywhere anywhere
Chain s0 (1 references) target prot opt source destination f0to1 all -- anywhere 192.168.1.101 f0to1 all -- anywhere 192.168.1.255 f0to1 all -- anywhere xtech logdrop all -- anywhere anywhere
Chain s1 (1 references) target prot opt source destination f1to0 all -- anywhere anywhere
Chain srcfilt (2 references) target prot opt source destination s0 all -- anywhere anywhere
Knute Johnson - 07 Dec 2005 16:57 GMT >>>Assuming that multicasting uses UDP (is that correct?) I added a UDP port >>>matching the port used in our multicasting test program via Guarddog. [quoted text clipped - 15 lines] > isn't working, perhaps I should take this question to a more appropriate > forum rather than taking more of your time. If so, just let me know.) Probably a good idea to ask somebody that knows something about Guarddog, cause I don't have a clue. You are right though, there is not a chain in there with UDP port 12345. I don't know if you are running Fedora or not but there is a good list at http://www.redhat.com/mailman/listinfo/fedora-list They'll help even if you are running some other linux.
 Signature Knute Johnson email s/nospam/knute/
rick - 07 Dec 2005 17:19 GMT Knute,
Thank you for your help. I had no idea what the problem was and you've given me the info needed to get a solution. I really appreciate it. FYI, I'm using MEPIS, a Debian derivative.
Rick
Knute Johnson - 08 Dec 2005 02:37 GMT > Knute, > [quoted text clipped - 3 lines] > > Rick Don't hesitate to ask questions anytime. I haven't heard of MEPIS but there may be a list specific to them too.
 Signature Knute Johnson email s/nospam/knute/
Knute Johnson - 06 Dec 2005 17:46 GMT > Knute, > [quoted text clipped - 12 lines] > > Rick You just need to open the port you are going to listen on. So if you are going to listen on port 12345 you need a chain on the INPUT side that has an ACCEPT for UDP on that port. The syntax of iptables is kind of complicated so it might be simpler to use your Guarddog program to open the UDP port. If that doesn't work, let me know and I'll fire up my Linux box and see what I can come up with.
 Signature Knute Johnson email s/nospam/knute/
Gordon Beaton - 05 Dec 2005 07:07 GMT > I'm trying to use Java multicasting on a single computer (Linux) for > testing purposes and can't get it to work. Your code works for me on a Fedora 2 machine (although I needed to declare a DatagramSocket for the server).
All I can suggest is that you run ethereal or tcpdump and see if that gives you any more information.
/gordon
 Signature [ do not email me copies of your followups ] g o r d o n + n e w s @ b a l d e r 1 3 . s e
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|