Sunday, November 30, 2014

Teacher Student Chat (With BroadCast and Multicast)

Here's a program to implement Broadcast and Multicast Query System Between a student and a teacher.
You'll have to change the IP address per student logged in.
Just a basic outline for those who have doubts. UI isn't great.

TEACHER


import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;

public class teacher extends Frame implements ActionListener{
TextField tf2;
TextArea ta;
Button b1,b2;
TextField tf1;
public static void main(String args[])
{
teacher teach=new teacher();
}

public teacher()
{
Frame f1=new Frame();
Panel p1=new Panel();
Panel p2=new Panel();
GridLayout gl=new GridLayout(2,2);
FlowLayout fl=new FlowLayout();
p1.setLayout(gl);
tf1=new TextField();
b1=new Button("MULTICAST");
tf2=new TextField();
b2=new Button("BROADCAST");
b2.addActionListener(this);
b1.addActionListener(this);
p1.add(tf1);
p1.add(tf2);
p1.add(b1);
p1.add(b2);
ta=new TextArea();
p2.add(ta);
f1.setLayout(fl);
f1.add(p1,BorderLayout.NORTH);
f1.add(p2,BorderLayout.SOUTH);
f1.setSize(600,600);
f1.setVisible(true);
receiverfunc();


}

public void itsbroadcast()
{
try{
MulticastSocket ms=new MulticastSocket(3000);
InetAddress iads=InetAddress.getByName("255.255.255.255");
System.out.println("HI");
String str=tf2.getText().toString();
byte[] b=str.getBytes();
DatagramPacket dp=new DatagramPacket(b,str.length(),iads,3000);
ms.send(dp);
}catch(IOException e){}
}

public void itsmulticast()
{
try{
MulticastSocket ms=new MulticastSocket(3000);
InetAddress iads=InetAddress.getByName("231.4.2.3");
System.out.println("HI");
String str=tf1.getText().toString();
byte[] b=str.getBytes();
DatagramPacket dp=new DatagramPacket(b,str.length(),iads,3000);
ms.send(dp);
}catch(IOException e){}
}

public void receiverfunc()
{
try{
System.out.println("hi");
MulticastSocket ms=new MulticastSocket(3000);
InetAddress iads=InetAddress.getByName("231.4.2.2");
ms.joinGroup(iads);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
byte[] b=new byte[100];
DatagramPacket dp=new DatagramPacket(b,b.length);
ms.receive(dp);
String str=new String(dp.getData());
ta.append(str+"\n");
receiverfunc();
}catch(IOException e){}

}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b2)
itsbroadcast();
else if(e.getSource()==b1)
itsmulticast();
}
}




STUDENT

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;

public class student1 extends Frame implements ActionListener{
TextArea ta;
TextField tf1;
public static void main(String args[])
{
student1 teach=new student1();
}

public student1()
{
Frame f1=new Frame();
Panel p1=new Panel();
Panel p2=new Panel();
GridLayout gl=new GridLayout(2,2);
FlowLayout fl=new FlowLayout();
p1.setLayout(gl);
tf1=new TextField();
Button b1=new Button("SEND");
p1.add(tf1);
p1.add(b1);
b1.addActionListener(this);
ta=new TextArea();
p2.add(ta);
f1.setLayout(fl);
f1.add(p1,BorderLayout.NORTH);
f1.add(p2,BorderLayout.SOUTH);
f1.setSize(600,600);
f1.setVisible(true);
itsbroadcast();

}
public void itsbroadcast()
{
try{
System.out.println("hi");
MulticastSocket ms=new MulticastSocket(3000);
InetAddress iads=InetAddress.getByName("231.4.2.3");
ms.joinGroup(iads);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
byte[] b=new byte[100];
DatagramPacket dp=new DatagramPacket(b,b.length);
ms.receive(dp);
String str=new String(dp.getData());
ta.append(str+"\n");
itsbroadcast();
}catch(IOException e){}

}

public void itssent()
{
try{
MulticastSocket ms=new MulticastSocket(3000);
InetAddress iads=InetAddress.getByName("231.4.2.2");
System.out.println("HI");
String str=tf1.getText().toString();
byte[] b=str.getBytes();
DatagramPacket dp=new DatagramPacket(b,str.length(),iads,3000);
ms.send(dp);
}catch(IOException e){}
}

public void actionPerformed(ActionEvent e)
{
itssent();
}
}

No comments:

Post a Comment