Screenshot of broadcast server and clients using java |
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.ScrollPaneConstants; import javax.swing.WindowConstants; public class BroadcastProgram extends JFrame{ JScrollPane jsp; JTextArea ta; JLabel label; JTextField txtin; JButton send; MulticastSocket msoc; InetAddress group; public BroadcastProgram(boolean server) { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); try { group=InetAddress.getByName("239.255.1.2"); } catch (UnknownHostException ex) {ex.printStackTrace();} jsp=new JScrollPane(); setSize(328,415); setLayout(null); ta=new JTextArea(); ta.setEditable(false); label=new JLabel(); add(label); label.setBounds(5, 5, 300, 20); jsp.setViewportView(ta); add(jsp); jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); if(server) { label.setText("Enter broadcast messages:"); txtin=new JTextField(); txtin.setBounds(5,30, 220, 25); add(txtin); send=new JButton("Send"); add(send); send.setBounds(235,30,70,25); jsp.setBounds(5,65,300,300); try { msoc=new MulticastSocket(); } catch (IOException ex) {ex.printStackTrace();} ActionListener al=new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String s=txtin.getText(); DatagramPacket p=new DatagramPacket(s.getBytes(),s.length(),group,33333); try { msoc.send(p); txtin.setText(""); ta.setText(ta.getText()+s+"\n"); } catch (IOException ex) {ex.printStackTrace();} } }; send.addActionListener(al); } else { label.setText("Messages from broadcast server:"); jsp.setBounds(5,30, 300,360); try { msoc=new MulticastSocket(33333); receiver.start(); } catch (IOException ex) {ex.printStackTrace();} } } public static void main(String args[]) { int i=JOptionPane.showConfirmDialog(null,"Start Broadcast server?\nYes - Start broadcast server\nNo- Start a client","Start Broadcast server?",JOptionPane.YES_NO_OPTION); if(i==0) { new BroadcastProgram(true).setVisible(true); } else if(i==1) { new BroadcastProgram(false).setVisible(true); } } Thread receiver=new Thread(){ public void run() { try { msoc.joinGroup(group); while(true) { byte[] b=new byte[200]; DatagramPacket p=new DatagramPacket(b,200); msoc.receive(p); String s=new String(p.getData()); if(s.length()>0) ta.setText(ta.getText()+s+"\n"); } } catch (IOException ex) {ex.printStackTrace();} } }; }
In 31st line, we have created an InetAddress object which represent a multicast IP address 239.255.1.2. The server sends the broadcast or multicast packets to this IP and the clients listens to a multicast socket with this IP. In our program, the clients listen to port 33333. So, the server must send the broadcast messages to 239.255.1.2:33333. The clients join the broadcast group with IP 239.255.1.2 and receives packets at port 33333. This single java program can run either as java broadcast server or as a client. To try running this program, first run the program, select to run as server when you are prompted. Then run more instances of the same program but select to run as client when prompted.
In 55th line, a MulticastSocket msoc is created which is later used to send broadcast datagram packets. In 62nd line, when the send button is pressed the text in the textbox is sent as datagram packet using MulticastSocket object msoc to group (239.255.1.2) at port 33333. In 77th line the client creates a MulticastSocket object msoc which listens to port 33333. Client then starts a thread which first joins to the group by invoking msoc.joinGroup(group). Then in a loop client receives datagram packets from the group at port 33333 and adds message to the JTextArea object ta.
No comments:
Post a Comment