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><UP,LOOPBACK,RUNNING,MULTICAST> 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><UP,BROADCAST,b6,RUNNING,SIMPLEX,MULTI CAST> 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 <half-duplex> 10baseT/UTP <full-duplex> 100baseTX <half-duplex> 100baseTX <full-duplex>
ppp0: flags=<font color = blue>8051</font><UP,POINTOPOINT,RUNNING,MULTICAST& gt; mtu <font color = blue>1500</font>
inet <font color = blue>129.49</font>.<font color = blue>78.230</font> --> <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 - <ip> <port>
*/</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 <IP "</font>
+ <font color = red>"addr> <port>\n\t<IP addr> 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 - <ip> <port>
*/</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]