Tuesday, April 26, 2011

Client Server GUI Chating Application in Java

Categories:


This is an fine example for Scoket Programmin in java. This application contains 2 java classes. One is for server and other is for client. Java Scoket is used to connect them together. Each line of the code are explained through comments. To run this application first run the server one and then client. Two Gui will be shown as in below screenshorts.

AppServer.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
/**
 * @author Tapas
 * @mail tapas.friends@gmail.com
 * @blog tapas4web.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();
 }
} 

AppClient.java
import java.awt.*;
import java.awt.event.*;

import java.io.*;
import java.net.*;
/**
 * @author Tapas
 * @mail tapas.friends@gmail.com
 * @blog tapas4web.blogspot.com
 */
public class AppClient extends Frame implements ActionListener,Runnable
{
 //Declarations
 Button b;
 TextField tf;
 TextArea ta;
 Socket s;
 PrintWriter pw;
 BufferedReader br;
 Thread th;
 
 public AppClient()
 {
  Frame f=new Frame("Client Side Chatting");//Frame for Client
  f.setLayout(new FlowLayout());//set layout
  f.setBackground(Color.orange);//set background color of the Frame
  b=new Button("Send");//Send Button
  b.addActionListener(this);//Add action listener to send button.
  f.addWindowListener(new W1());//add Window Listener to the Frame
  tf=new TextField(15);
  ta=new TextArea(12,20);
  ta.setBackground(Color.cyan);
  f.add(tf);//Add TextField to the frame
  f.add(b);//Add send Button to the frame
  f.add(ta);//Add TextArea to the frame
  try{
   s=new Socket(InetAddress.getLocalHost(),12000);//Socket for client
   //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.setVisible(true);
  f.setLocation(100,300);//set the location
  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{
    ta.append(br.readLine()+"\n");//Append to TextArea
   }catch(Exception e) {}
  }
 }
 //Main method
 public static void main(String args[])
 {
  //Instantiate AppClient class
  AppClient client = new AppClient();
 }
}

The above prograam looks like below -



Spread The Love, Share Our Article

Related Posts

8 Response to Client Server GUI Chating Application in Java

Anonymous
February 20, 2012 at 11:10 AM

how to run this code?

July 12, 2012 at 11:35 AM

thanks very much
ifeel very happy sir

December 1, 2012 at 9:43 PM

hyyy.....i am so happy ...it helps alot and GOD bless u dear.

Anonymous
December 25, 2012 at 1:09 AM

thanks for your code. I tried this on netbeans but wont run, so I used eclipse. Anyways, can you share your knowledge on including databases on this application? thanks a lot.

Anonymous
December 31, 2012 at 9:57 AM

nice.! Help a lot in many projects.!

What about if we wanted more clients ??

July 25, 2014 at 4:41 AM

Awsome bro , God bless u

December 30, 2016 at 7:05 AM

fantastic code helped alot

January 1, 2017 at 8:52 AM

Thanksss

Post a Comment