Chating Program with GUI in JAVA

Chating Program with GUI in JAVA :

Client Program:

AppClient.java
______________________________________________________________________________

import java.awt.*;

import java.awt.event.*;
import java.io.*;
import java.net.*;

class AppClient extends Frame implements ActionListener,Runnable

{
Button b;
TextField tf;
TextArea ta;
 Socket s;
 PrintWriter pw;
 BufferedReader br;
 Thread th;
 public AppClient()
 {
  Frame f=new Frame("Client Side Chatting");
  f.setLayout(new FlowLayout());
  f.setBackground(Color.orange);
  b=new Button("Send");
  b.addActionListener(this);
  f.addWindowListener(new W1());
  tf=new TextField(15);
  ta=new TextArea(12,20);
  ta.setBackground(Color.cyan);
  f.add(tf);
  f.add(b);
  f.add(ta);
  try{
   s=new Socket(InetAddress.getLocalHost(),12000);
   br=new BufferedReader(new InputStreamReader(s.getInputStream()));
   pw=new PrintWriter(s.getOutputStream(),true);
  }
  catch(Exception e){}
  th=new Thread(this);
  th.setDaemon(true);
  th.start();
  setFont(new Font("Arial",Font.BOLD,20));
  f.setSize(200,200);
  f.setVisible(true);
  f.setLocation(100,300);
  f.validate();
 }
 private class W1 extends WindowAdapter
 {
  public void windowClosing(WindowEvent we)
  {
   System.exit(0);
  }
 }
 public void actionPerformed(ActionEvent ae)
 {
  pw.println(tf.getText());
  tf.setText("");
 }
 public void run()
 {
  while(true)
  {
   try{
    ta.append(br.readLine()+"\n");
   }catch(Exception e) {}
  }
 }
 public static void main(String args[])
 {
  AppClient client = new AppClient();
 }
}
______________________________________________________________________________


Server Program:

AppServer.java
______________________________________________________________________________

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
/**
 * @author Suraj
 * @blog surajzanvar.blogspot.com
 */
public class AppServer extends Frame implements ActionListener,Runnable
{
 //Declarations
 Button b1;
 TextField tf;
 TextArea ta;
 ServerSocket ss;
 Socket s;
 PrintWriter pw;
 BufferedReader br;
 Thread th;
 public AppServer()
 {
  Frame f=new Frame("Server Side Chatting");//Frame for Server
  f.setLayout(new FlowLayout());//set layout
  f.setBackground(Color.orange);//set background color of the Frame
  b1=new Button("Send");//Send Button
  b1.setBackground(Color.pink);
  b1.addActionListener(this);//Add action listener to send button.
  tf=new TextField(15);
  ta=new TextArea(12,20);
  ta.setBackground(Color.cyan);
  f.addWindowListener(new W1());//add Window Listener to the Frame
  f.add(tf);//Add TextField to the frame
  f.add(b1);//Add send Button to the frame
  f.add(ta);//Add TextArea to the frame
  try{
   ss=new ServerSocket(12000);//Socket for server
   s=ss.accept();//accepts request from client
   System.out.println(s);
   //below line reads input from InputStreamReader
   br=new BufferedReader(new InputStreamReader(s.getInputStream()));
   //below line writes output to OutPutStream
   pw=new PrintWriter(s.getOutputStream(),true);
  }catch(Exception e)
  {
  }
  th=new Thread(this);//start a new thread
  th.setDaemon(true);//set the thread as demon
  th.start();
  setFont(new Font("Arial",Font.BOLD,20));
  f.setSize(200,200);//set the size
  f.setLocation(300,300);//set the location
  f.setVisible(true);
  f.validate();
 }
 //method required to close the Frame on clicking "X" icon.
 private class W1 extends WindowAdapter
 {
  public void windowClosing(WindowEvent we) 
  {
   System.exit(0);
  }
 }
 //This method will called after clicking on Send button.
 public void actionPerformed(ActionEvent ae)
 {
  pw.println(tf.getText());//write the value of textfield into PrintWriter
  tf.setText("");//clean the textfield
 }
 //Thread running as a process in background
 public void run()
 {
  while(true)
  {
   try{
    String s=br.readLine();//reads the input from textfield
    ta.append(s+"\n");//Append to TextArea
   }catch(Exception e)
   {
   }
  } 
 }
 //Main method
 public static void main(String args[]) 
 {
  //Instantiate AppServer class
  AppServer server = new AppServer();
 }

______________________________________________________________________________

No comments:

Post a Comment