Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

broadcasting a UDP Datagram Packet over 255.255.255.255

807591May 20 2008 — edited May 22 2008
I am building a very simple intranet application (yes, that's inTRAnet), where multiple PCs over a simple local network can communicate freely via a SWF (Flash) file. I'm building the XML server in JAVA, which is meant to run as a single instance on each PC, communicating with a local SWF, and broadcasting necessary data to it's community of JAVA XML server instances.

I have the Flash to JAVA socket stuff down. I'm now working on JAVA app to JAVA app communication. The concept here is a little different from the norm, in that there is no sever-client relationship. Each instance of the JAVA app is the same, therefore everyone is a client AND a server. The idea is, when the JAVA app receives a command from the Flash app, it broadcasts the data over the network, and then ALL instances of the JAVA app (including the sender) receive the data, and pass it on to their corresponding Flash app, that decides what to do with the data. The concept is very similar to a chat room.

I've looked through enough threads to know that this can be accomplished in various ways. I've ruled out Multicasting, due to the IP address limitation (restricted to Class D). I think I'm on the right track using a Datagram Socket and sending a UDP Datagram Packet over that socket through the 'broadcast" IP (255.255.255.255). However, I've tried all sorts of things, and keep getting an error from JAVA... so I'm sort of at a dead end.

Below, is the code to the main thread class (where I last left it):

{code}

import java.io.*;
import java.net.*;
import java.util.*;

public class MyThread extends Thread
{
private int ticker = 1;
protected DatagramSocket socket = null;

public void myThread() throws IOException
{
DatagramSocket socket = new DatagramSocket(null);
socket.setBroadcast(true);
socket.bind(new InetSocketAddress(10000));
}

public void run()
{
while(ticker < 50)
{
try
{
byte[] buf = new byte[256];
String myString = Integer.toString(ticker);
buf = myString.getBytes();
InetAddress group = InetAddress.getByName("255.255.255.255");
DatagramPacket packet = new DatagramPacket(buf, buf.length, group, 10000);
socket.send(packet);
ticker ++;
try
{
sleep((long)(1000));
}
catch (InterruptedException e)
{

}
}
catch (IOException e)
{

}
}
}
}

{code}

It's a simple attempt to broadcast a ticker value every second until that ticker reaches a value of 50. I am simply trying to get the app to broadcast at this point.... with the plan to work on the receiving end afterwards. When run, it yields the following error (fails at the socket.send command):

Exception in thread "Thread-0" java.lang.NullPointerException
at MyThread.run(MyThread.java:38)

Bear in mind that this class is run from the main class of the app. Also, bear in mind that I'm new to JAVA and somewhat of a hack. Any suggestions would be very appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 19 2008
Added on May 20 2008
20 comments
2,329 views