AIM
Write a program for implement Public chat using JAVA
PROGRAM
//publicchatC.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class publicchatC extends Frame implements ActionListener
{
TextArea txt1,txt2;
Button btn;
String msg;
PrintWriter pw;
Socket s;
public publicchatC()
{
setLayout(new GridLayout(3,1));
txt1=new TextArea();
btn=new Button("Send");
txt2=new TextArea();
add(txt1);
add(btn);
add(txt2);
txt1.setText("");
txt2.setText("");
btn.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
setVisible(false);
}
});
}
public void setConnection()
{
try
{
s=new Socket("localhost",6969);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(s.getOutputStream());
while(true)
{
msg=br.readLine();
txt2.append("Rec:"+msg+"\n");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void actionPerformed(ActionEvent ae)
{
try
{
if(ae.getSource()==btn)
{
msg=txt1.getText();
txt2.append("Send:"+msg+"\n");
pw.println(msg);
pw.flush();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String args[])throws Exception
{
publicchatC obj=new publicchatC();
obj.setSize(300,300);
obj.setVisible(true);
obj.setConnection();
}
}