1. repeatedly broadcast a piece of news.
Code:
- Package mulsocket. frombook;
- Import java.net .*;
- Public class broadcast extends thread {
- String S = "today's weather is fine, good scenery everywhere! ";
- Int Port = 5858; // The multicast port.
- Inetaddress group = NULL; // multicast group
- Multicastsocket socket = NULL; // multicast socket
- Broadcast (){
- Try {
- Group = inetaddress. getbyname ("239.20.8.0"); // you can specify 239.20.8.0 as the multicast group.
- Socket = new multicastsocket (port); // The multicast socket will be broadcast on the port.
- Socket. settimetolive (0); // The data packet sent by the Multi-Point broadcast socket is in the local network.
- Socket. joingroup (group );
- // Join the multicast group. After joining the group, the datagram sent by the socket can be received by the member added to the group.
- } Catch (exception e ){
- }
- }
- Public void run (){
- While (true ){
- Try {
- Datagrampacket packet = NULL; // The datagram to be broadcast
- Byte data [] = S. getbytes ();
- Packet = new datagrampacket (data, data. length, group, Port );
- System. Out. println (new string (data ));
- Socket. Send (packet); // broadcast Datagram
- Sleep (2000 );
- } Catch (exception e ){
- System. Out. println (E. tostring ());
- Break;
- }
- }
- }
- Public static void main (string ARGs []) {
- New broadcast (). Start ();
- }
- }
2. Join the multicast group to receive Broadcast Data
Code:
- Package mulsocket. frombook;
- Import java.net .*;
- Import java. AWT .*;
- Import java. AWT. event .*;
- Import javax. Swing .*;
- Public class receive extends jframe implements runnable, actionlistener {
- Private Static final long serialversionuid = 1l;
- Int port; // multicast Port
- Inetaddress group = NULL; // the address of the multicast group
- Multicastsocket socket = NULL; // multicast socket
- Jbutton startreceive, stopreceive;
- Jtextarea showarea;
- Thread thread; // The thread responsible for receiving information
- Boolean stop = false;
- Public receive (){
- Super ("timed receiving information ");
- Thread = new thread (this );
- Startreceive = new jbutton ("start receiving ");
- Stopreceive = new jbutton ("stop receiving ");
- Startreceive. addactionlistener (this );
- Stopreceive. addactionlistener (this );
- Showarea = new jtextarea (10, 10 );
- Jpanel North = new jpanel ();
- North. Add (startreceive );
- North. Add (stopreceive );
- Container con = getcontentpane ();
- Con. Add (North, borderlayout. North );
- Con. Add (New jscrollpane (showarea), borderlayout. center );
- Port = 5858;
- Try {
- // Initialize in one breath
- Group = inetaddress. getbyname ("239.20.8.0 ");
- Socket = new multicastsocket (port );
- Socket. joingroup (group );
- } Catch (exception e ){
- }
- Setdefaclocloseoperation (jframe. exit_on_close );
- Setsize (320,300 );
- Validate ();
- Setvisible (true );
- }
- Public void actionreceivmed (actionevent e ){
- If (E. getsource () = startreceive ){
- If (! (Thread. isalive ())){
- Thread = new thread (this );
- Stop = false;
- }
- Try {
- Thread. Start ();
- } Catch (exception ee ){
- }
- }
- If (E. getsource () = stopreceive ){
- Stop = true;
- }
- }
- Public void run (){
- While (true ){
- Byte data [] = new byte [8192];
- Required rampacket packet = NULL;
- Packet = new datagrampacket (data, data. length, group, Port );
- Try {
- Socket. Receive (packet );
- String message = new string (packet. getdata (), 0, packet
- . Getlength ());
- Showarea. append ("/N" + message );
- Showarea. setcaretposition (showarea. gettext (). Length ());
- } Catch (exception e ){
- }
- If (stop = true)
- Break;
- }
- }
- Public static void main (string ARGs []) {
- New receive ();
- }
- }
The idea is clear and easy to understand.