Reputation: 267
I've noticed how routers can send a broadcast packet within an area, to allow a Wireless Network Connection, and hence network access. My question is how can we implement something like this in java? I know how I can "UDP Broadcast", but that's not specifically the type of broadcast I want to do.
import java.net.*;
class Broadcast{ //Example of UDP Broadcast Server
public static void main(String[] args){
DatagramSocket broadcastserver = new DatagramSocket(80);
broadcastserver.setBroadcast(true);
...
...
In conclusion, instead of broadcasting on an port like UDP broadcast can achieve, how can we broadcast within an area like what a router can do.
Upvotes: 4
Views: 550
Reputation: 3638
If you mean raw ethernet broadcast you cannot do that from pure Java. (BTW, is your question a duplicate of this: How can I read or get the information of the beacon frame that sent by the Access point in WLAN, by java or Android? )
For raw sockets, a straight-forward way (if you know what platform(s) you will execute on) is to do it directly in native code, with a suitable interface to the rest of your Java system.
A more complex option is to wrap the native code in a subclass to java.net.Socket, which gives a standard interface towards the rest of the code but may be tricky depending on how much of the Socket interface you want to implement, and how much you can afford relying on convensions for addressing and that the calling code is correct.
A third option is to use existing libraries for raw ethernet, see e.g. the answers to
For a brief discussion of privileges for opening a raw socket, see e.g., send/receiving raw ethernet frames
Update: Some more details on ethernet (programming) re: question in comment.
First, the Wikipedia article about the ethernet frame is a good introduction to the protocol: http://en.wikipedia.org/wiki/Ethernet_frame .
About programming, the boring answer is to read about SOCK_RAW, starting with the man page for socket(2). After creating a raw socket, you basically create a frame (as a byte array) and send it using either write(2) or sendto(2) and read with read(2) or recvfrom(2). Use the wikipedia article for getting the format right when creating the ethernet frame.
For programming in Python, the top 3 google results for "raw ethernet python" I get is
and that seems like a reasonable starting point.
For a more bit more comprehensive article, http://www.binarytides.com/python-packet-sniffer-code-linux/ may be worth a read.
Upvotes: 2