Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Need help with Java MulticastSocket (http://www.velocityreviews.com/forums/t148277-need-help-with-java-multicastsocket.html)

rick 12-03-2005 04:55 PM

Need help with Java MulticastSocket
 
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/tutor...adcasting.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 12-03-2005 05:58 PM

Re: Need help with Java MulticastSocket
 
rick wrote:
> 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/tutor...adcasting.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


Rick:

Are you running IPTABLES? Could that have a chain that is blocking it?

--

Knute Johnson
email s/nospam/knute/

rick 12-03-2005 09:05 PM

Re: Need help with Java MulticastSocket
 
On Sat, 03 Dec 2005 09:58:01 -0800, Knute Johnson wrote:

> rick wrote:
>> 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/tutor...adcasting.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

>
> Rick:
>
> 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 12-04-2005 06:24 PM

Re: Need help with Java MulticastSocket
 
rick wrote:
> On Sat, 03 Dec 2005 09:58:01 -0800, Knute Johnson wrote:
>
>
>>rick wrote:
>>
>>>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/tutor...adcasting.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

>>
>>Rick:
>>
>>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.


I don't see anything wrong, why don't you post a compilable test that
doesn't work so we can try it.

--

Knute Johnson
email s/nospam/knute/

rick 12-04-2005 08:41 PM

Re: Need help with Java MulticastSocket
 
On Sun, 04 Dec 2005 10:24:31 -0800, Knute Johnson wrote:

> rick wrote:
>> On Sat, 03 Dec 2005 09:58:01 -0800, Knute Johnson wrote:
>>
>>
>>>rick wrote:
>>>
>>>>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/tutor...adcasting.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
>>>
>>>Rick:
>>>
>>>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.

>
> 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/tutor...adcasting.html

Files:

http://java.sun.com/docs/books/tutor...astServer.java
http://java.sun.com/docs/books/tutor...verThread.java
http://java.sun.com/docs/books/tutor...astClient.java
http://java.sun.com/docs/books/tutor...verThread.java
http://java.sun.com/docs/books/tutor...one-liners.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 12-05-2005 01:52 AM

Re: Need help with Java MulticastSocket
 
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();
}
}

--

Knute Johnson
email s/nospam/knute/

Gordon Beaton 12-05-2005 07:07 AM

Re: Need help with Java MulticastSocket
 
On Sat, 03 Dec 2005 11:55:35 -0500, rick wrote:
> 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

--
[ 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

rick 12-05-2005 01:36 PM

Re: Need help with Java MulticastSocket
 
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 12-05-2005 04:42 PM

Re: Need help with Java MulticastSocket
 
rick wrote:
> 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.

--

Knute Johnson
email s/nospam/knute/

rick 12-06-2005 12:15 PM

Re: Need help with Java MulticastSocket
 
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





All times are GMT. The time now is 01:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.