Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > JAVA / MulticastSocket / multicasting

JAVA / MulticastSocket / multicasting
Thread Tools
Fresh-Faced Recruit
Join Date: Nov 2001
Status: Offline
Reply With Quote
Nov 9, 2001, 07:51 AM
 
Hi,
I am attempting to develop a program that uses multicasting on MacOS 10.1. I found sample programs (MultiCastReceiver/MultiCastSender) of how to do this on the web (at http://sunsite.net.edu.cn/tutorials/...e/24javafi.htm). I verified that these programs do in fact work correctly on another computer (running FreeBSD).

MultiCastReceiver takes a multicast IP and port number, and listens on it until something is received, and once it is, it is displayed to standard out. Below you can see the error I got when attempting to run it. The corresponding MultiCastSender program gives no error when running it.

% java MultiCastReceiver 225.6.2.3 5000
Unable to obtain socket: java.net.SocketException: error setting options
%


After searching the web for a while, I found two things someone suggests to look for when running across this error message:

1) make sure the word "MULTICAST" comes up for every entry when I type "ifconfig -a". It looks like this was alright (I think).

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
% ifconfig -a
lo0: flags=<font color = blue>8049</font>&lt;UP,LOOPBACK,RUNNING,MULTICAST&gt; mtu <font color = blue>16384</font>
inet <font color = blue>127.0</font>.<font color = blue>0.1</font> netmask 0xff000000
en0: flags=<font color = blue>8863</font>&lt;UP,BROADCAST,b6,RUNNING,SIMPLEX,MULTI CAST&gt; mtu <font color = blue>1500</font>
ether <font color = blue>00</font>:0a:<font color = blue>27</font>:b4:9a:<font color = blue>18</font>
media: autoselect (none) status: inactive
supported media: none autoselect 10baseT/UTP &lt;half-duplex&gt; 10baseT/UTP &lt;full-duplex&gt; 100baseTX &lt;half-duplex&gt; 100baseTX &lt;full-duplex&gt;
ppp0: flags=<font color = blue>8051</font>&lt;UP,POINTOPOINT,RUNNING,MULTICAST& gt; mtu <font color = blue>1500</font>
inet <font color = blue>129.49</font>.<font color = blue>78.230</font> --&gt; <font color = blue>129.49</font>.<font color = blue>76.15</font> netmask 0xffff0000
%
</font>[/code]


2) add a route if there is not one when listing "netstat -r".

There wasn't a route, so I had to add one:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
% sudo route add <font color = blue>225.6</font>.<font color = blue>2.3</font> localhost
Password:
add host <font color = blue>225.6</font>.<font color = blue>2.3</font>: gateway localhost
% netstat -r
Routing tables

Internet:
Destination Gateway Flags Refs Use Netif Expire
default as14.dialup.sunysb UGSc <font color = blue>39</font> <font color = blue>10</font> ppp0
localhost localhost UH <font color = blue>6</font> <font color = blue>6816</font> lo0
as14.dialup.sunysb <font color = blue>078</font>-<font color = blue>103.</font>dialup.sun UH <font color = blue>40</font> <font color = blue>0</font> ppp0
<font color = blue>225.6</font>.<font color = blue>2.3</font> localhost UGHS <font color = blue>0</font> <font color = blue>0</font> lo0
%
</font>[/code]


However, this did not fix the problem.

Does anyone know what the problem could be? I am surprised after searching the web I did not see any questions about this anywhere, and nothing on Apple's website. Perhaps I'm the only one having this problem for some reason. Below is the code for MultiCastReceiver (because when I checked the URL I posted above a minute ago, it didn't load). If anyone has time, could you check to see if it works on your computer?

Thanks.
--Daniel

_________________________________________________
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
<font color = brown>//import sun.net.*; // This will move to java.net soon.</font>
import java.net.*; <font color = brown>// Import package names used.</font>
import java.lang.*;
import java.io.*;
<font color = brown>/**
* This is a program which allows you to listen
* at a particular multicast IP address/port and
* print out incoming UDP datagrams.
* @author David W. Baker
* @version <font color = blue>1.1</font>
*/</font>
class MultiCastReceiver {
<font color = brown>// The length of the data portion of incoming</font>
<font color = brown>// datagrams.</font>
private static final int DATAGRAM_BYTES = <font color = blue>256</font>;
private int mcastPort;
private InetAddress mcastIP;
private MulticastSocket mcastSocket;
<font color = brown>// Boolean to tell the client to keep looping for</font>
<font color = brown>// new datagrams.</font>
private boolean keepReceiving = true;
<font color = brown>/**
* This starts up the application
* @param args Program arguments - &lt;ip&gt; &lt;port&gt;
*/</font>
public static void main(String[] args) {
<font color = brown>// This must be the same port and IP address</font>
<font color = brown>// used by the sender.</font>
if (args.length != <font color = blue>2</font>) {
System.out.print(<font color = red>"Usage: MultiCastReceiver &lt;IP "</font>
+ <font color = red>"addr&gt; &lt;port&gt;\n\t&lt;IP addr&gt; can be one of "</font>
+ <font color = red>"<font color = blue>224.</font>x.x.x - <font color = blue>239.</font>x.x.x\n"</font>);
System.exit(<font color = blue>1</font>);
}
MultiCastReceiver send = new MultiCastReceiver(args);
System.exit(<font color = blue>0</font>);
}
<font color = brown>/**
* The constructor does the work of opening a socket,
* joining the multicast group, and printing out
* incoming data.
* @param args Program arguments - &lt;ip&gt; &lt;port&gt;
*/</font>
public MultiCastReceiver(String[] args) {
DatagramPacket mcastPacket; <font color = brown>// Packet to receive.</font>
byte[] mcastBuffer; <font color = brown>// byte[] array buffer</font>
InetAddress fromIP; <font color = brown>// Sender address.</font>
int fromPort; <font color = brown>// Sender port.</font>
String mcastMsg; <font color = brown>// String of message.</font>
try {
<font color = brown>// First, set up our receiving socket.</font>
mcastIP = InetAddress.getByName(args[<font color = blue>0</font>]);
mcastPort = Integer.parseInt(args[<font color = blue>1</font>]);
mcastSocket = new MulticastSocket(mcastPort);
<font color = brown>// Join the multicast group.</font>
mcastSocket.joinGroup(mcastIP);
} catch(UnknownHostException excpt) {
System.err.println(<font color = red>"Unknown address: "</font> + excpt);
System.exit(<font color = blue>1</font>);
} catch(SocketException excpt) {
System.err.println(<font color = red>"Unable to obtain socket: "</font>
+ excpt);
System.exit(<font color = blue>1</font>);
} catch(IOException e) {
System.out.println(<font color = red>"IOException \n"</font> + e);
}

while (keepReceiving) {
try {
<font color = brown>// Create a new datagram.</font>
mcastBuffer = new byte[DATAGRAM_BYTES];
mcastPacket = new DatagramPacket(mcastBuffer,
mcastBuffer.length);
<font color = brown>// Receive the datagram.</font>
mcastSocket.receive(mcastPacket);
fromIP = mcastPacket.getAddress();
fromPort = mcastPacket.getPort();
mcastMsg = new String(mcastPacket.getData(),<font color = blue>0</font>);
<font color = brown>// Print out the data.</font>
System.out.println(<font color = red>"Received from "</font> + fromIP +
<font color = red>" on port "</font> + fromPort + <font color = red>": "</font> + mcastMsg);
} catch(IOException excpt) {
System.err.println(<font color = red>"Failed I/O: "</font> + excpt);
}
}
try {
mcastSocket.leaveGroup(mcastIP); <font color = brown>// Leave the group.</font>
} catch(SocketException excpt) {
System.err.println(<font color = red>"Socket problem leaving group: "</font>
+ excpt);
} catch(IOException e) {
System.out.println(<font color = red>"IOException \n"</font> + e);
}
mcastSocket.close(); <font color = brown>// Close the socket.</font>
}
<font color = brown>/**
* This method provides a way to stop the program.
*/</font>
public void stop() {
if (keepReceiving) {
keepReceiving = false;
}
}
}
</font>[/code]
     
Fresh-Faced Recruit
Join Date: Dec 1999
Location: mars,ca,usa
Status: Offline
Reply With Quote
Nov 10, 2001, 11:12 AM
 
Might want to try "netstat -g". It should show you more on the multicast routing table.

Also, here are some useful links. Its c but it might give you some insight. The network you are running on must be multicast enabled. Did not see you set a ttl in your code. Also, might want to be careful about exceptions throwing you out of your while loop.

setting up routes:
http://notch.mathstat.muohio.edu/htm...t-HOWTO-3.html

general multicast info:
http://notch.mathstat.muohio.edu/htm...OWTO.html#toc3

Hope this helps..
     
Junior Member
Join Date: Apr 2001
Location: Sunnyvale, CA
Status: Offline
Reply With Quote
Nov 10, 2001, 02:38 PM
 
I'm not familiar with multicasting or the particular program, but I've done some socket programming in various languages (including Java) so hopfully I'm not too far off mark...

The literal interpretation of the error is that the program is unable to obtain a socket connection to the IP 225.6.2.3 at socket 5000.
This means:
1) the IP is invalid,
2) port 5000 on the specified IP is not broadcasting or otherwise open.

Are you sure the IP and port number is correct? Try "telnet 225.6.2.3 5000" and see if it lets you establish a connection.
     
Fresh-Faced Recruit
Join Date: Nov 2001
Status: Offline
Reply With Quote
Nov 11, 2001, 09:29 AM
 
Thanks for the tip!
When I run "netstat -g", it says nothing is in my multicast routing table (see below). Any idea why this is happening?
Thanks,
--Daniel

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
% sudo route add -net <font color = blue>224.0</font>.<font color = blue>0.0</font> -netmask <font color = blue>240.0</font>.<font color = blue>0.0</font> -host localhost
Password:
add net <font color = blue>224.0</font>.<font color = blue>0.0</font>: gateway localhost
% netstat -r
Routing tables

Internet:
Destination Gateway Flags Refs Use Netif Expire
default as14.dialup.sunysb UGSc <font color = blue>7</font> <font color = blue>7</font> ppp0
localhost localhost UH <font color = blue>10</font> <font color = blue>16660</font> lo0
as14.dialup.sunysb <font color = blue>078</font>-<font color = blue>115.</font>dialup.sun UH <font color = blue>8</font> <font color = blue>0</font> ppp0
<font color = blue>224</font>/<font color = blue>4</font> localhost UGS <font color = blue>0</font> <font color = blue>0</font> lo0
% netstat -g

Virtual Interface Table is empty

Multicast Routing Table is empty

%
</font>[/code]


Originally posted by Jablabla:
<STRONG>Might want to try "netstat -g". It should show you more on the multicast routing table.

Hope this helps.. </STRONG>
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:49 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2