Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Chatserver Client Side Application

Related Tutorials


 
 

Share on Google+Share on Google+

Client Side Application

Advertisement
For creating the Client side application firstly creates the login frame it consist one textfield and the login button. After hitting the login button it shows the next frame that Client Frame and it consist one textfield for writing the message and one s

Client Side Application

     

For creating the Client side application firstly creates the login frame it consist one textfield and the login button. After hitting the login button it shows the next frame that Client Frame and it consist one textfield for writing the message and one send button for sending it. And two list boxes, one is for showing the all messages and another list box is use to show the all user names. This frame has one more button that is Logout button for terminating the chat.

For running the whole Client operation we can run the Login file (Login.java). It can directly call Client file (MyClient.java).

Here is the code of Login.java file :

/****************************************************************
*  Version  :  1.0
*  Date  :  02/03/2007
*  
*  Description
*  This is a Login frame of client side application in chat System.
*  It is used to just take the user name 
*  
*  Remarks
*  Before running the Login application make sure the server is 
*  running.

*  If server is running then only you can execute your application.
******************************************************************/

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

// Login class which takes a user name and passed it to client class
public class Login implements ActionListener{
  JFrame frame1;
  JTextField tf;
  JButton button;
  JLabel heading;
  JLabel label;
  public static void main(String[] args){
  new Login();
  }
  public Login(){
  frame1 = new JFrame("Login Page");
  tf=new JTextField();
  button=new JButton("Login");
  heading=new JLabel("Chat Server");
  heading.setFont(new Font("Impact", Font.BOLD,40));
  label=new JLabel("Enter you Login Name");
  label.setFont(new Font("Serif", Font.PLAIN, 24));
  JPanel panel = new JPanel();
  button.addActionListener(this);
  panel.add(heading);panel.add(tf);panel.add(label);
  panel.add
(button);
  heading.setBounds(30,20,280,80);
  label.setBounds(20,100,250,60);
  tf.setBounds(50,150,150,30);
  button.setBounds(70,200,90,30);
  frame1.add(panel);
  panel.setLayout(null);
  frame1.setSize(300300);
  frame1.setVisible(true);
  frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  // pass the user name to MyClient class
  public void actionPerformed(ActionEvent e){
  String name="";
  try{
  name=tf.getText();
  frame1.dispose();
  MyClient mc=new MyClient(name);
  }catch (IOException te){}
  }
}

Download this Code

Output of the code is :

The Client side application follows these steps :

  • In the client sides firstly creates a new socket and specifies the address and port of the server and establish the connection with the Server.
    Socket s=new Socket("localhost",1004);

    Note - Instead of "localhost" you have to write the server IP address or computer name.

     
  • After that client makes a new thread and DataInputStream, ObjectInputStream and DataOutputStream for sending the user name and retrieving the list of all users and add the all user name in its list box through iterator.
      DataOutputStream dout=new DataOutputStream(s.getOutputStream());
      dout.writeUTF(name);
      DataInputStream din1=new DataInputStream(s.getInputStream());
      ObjectInputStream obj=new ObjectInputStream(din1);
      ArrayList alname=new ArrayList();
      Alname=(ArrayList)obj.readObject();
      String lname;
      Iterator i1=alname.iterator();
      while(i1.hasNext()){
      lname=(String)i1.next();
      model1.addElement(lname);
      }

      
    Note ? model1 is the object of DefaultListModel that is used to add the element in JList Box.
     
  • Now we make one new thread for sending and receiving the messages from the server. It does this task by using DataInputStream and DataOutputStream.
      dout.writeUTF(str); // for sending the messages
      str1=din.readUTF(); // receiving the messages
      model.addElement(str1) // add these messages to JList Box
  • When the client is logged out it sends its name and message ?User_Name has Logged out? and terminate the chatting
      dout.writeUTF(name+" has Logged out");
          dlout.writeUTF(name);

Here is the code of Client Side application :

/****************************************************************
*  Version  :  1.0
*  Date   :  02/03/2007
*  
*  Description
*  This is a client side of chat application.
*  This application is used to sending and receiving the messages
*  and in this we can maintain the list of all online users
*  
*  Remarks
*  Before running the client application make sure the server is 
*  running.If server is running then only you can execute your
*  application.
******************************************************************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;

//create the GUI of the client side
public class MyClient extends WindowAdapter implements ActionListener{
  JFrame frame;
  JList list;
  JList list1;
  JTextField tf;
  DefaultListModel model;
  DefaultListModel model1;
  JButton button;
  JButton lout;
  JScrollPane scrollpane;
  JScrollPane scrollpane1;
  JLabel label;
  Socket s,s1,s2;
  DataInputStream din;
  DataOutputStream dout;
  DataOutputStream dlout;
  DataOutputStream dout1;
  DataInputStream din1;
  String name;
  
  MyClient(String name)throws IOException{
  frame = new JFrame("Client Side");
  tf=new JTextField();
  model=new DefaultListModel();
  model1=new DefaultListModel();
  label=new JLabel("Message");
  list=new JList(model);
  list1=new JList(model1);
  button=new JButton("Send");
  lout=new JButton("Logout");
  scrollpane=new JScrollPane(list);
  scrollpane1=new JScrollPane(list1);
  JPanel panel = new JPanel();
  button.addActionListener(this);
  lout.addActionListener(this);
  panel.add(tf);panel.add(button);panel.add(scrollpane);
  panel.add(label);panel.add(lout);
  panel.add(scrollpane1);
  scrollpane.setBounds(10,20,180,150);
  scrollpane1.setBounds(250,20,100,150);
  label.setBounds(20,180,80,30);
  tf.setBounds(100,180,140,30);
  button.setBounds(260,180,90,30);
  lout.setBounds(260,230,90,30);
  frame.add(panel);
  panel.setLayout(null);
  frame.setSize(400400);
 frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.name=name;
  frame.addWindowListener(this);
  s=new Socket("localhost",1004);  //creates a socket object
  s1=new Socket("localhost",1004);
  s2=new Socket("localhost",1004);
  //create inputstream for a particular socket
  din=new DataInputStream(s.getInputStream());
  //create outputstream
  dout=new DataOutputStream(s.getOutputStream());
  //sending a message for login
  dout.writeUTF(name+" has Logged in");  
  dlout=new DataOutputStream(s1.getOutputStream());
  dout1=new DataOutputStream(s2.getOutputStream());
  din1=new DataInputStream(s2.getInputStream());

// creating a thread for maintaning the list of user name
  My1 m1=new My1(dout1,model1,name,din1);
  Thread t1=new Thread(m1);
  t1.start();  
  //creating a thread for receiving a messages
  My m=new My(din,model);
  Thread t=new Thread(m);
  t.start();
  }
  public void actionPerformed(ActionEvent e){
  // sending the messages
  if(e.getSource()==button){  
  String str="";
  str=tf.getText();
  tf.setText("");
  str=name+": > "+str;
  try{
  dout.writeUTF(str);
  System.out.println(str);
  dout.flush();
  }catch(IOException ae){System.out.println(ae);}
  }
  // client logout
  if (e.getSource()==lout){
  frame.dispose();
  try{
 //sending the message for logout
  dout.writeUTF(name+" has Logged out");
  dlout.writeUTF(name);
  dlout.flush();
  Thread.currentThread().sleep(1000);
  System.exit(1);
  }catch(Exception oe){}
  }
  }
  public void windowClosing(WindowEvent w){
  try{
  dlout.writeUTF(name);
  dlout.flush();  
  Thread.currentThread().sleep(1000);
  System.exit(1);
  }catch(Exception oe){}
  }
}

// class is used to maintaning the list of user name
class My1 implements Runnable{
  DataOutputStream dout1;
  DefaultListModel model1;  
  DataInputStream din1;
  String name,lname;
  ArrayList alname=new ArrayList()//stores the list of user names
  ObjectInputStream obj; // read the list of user names
  int i=0;
  My1(DataOutputStream dout1,DefaultListModel model1,
   String name,DataInputStream din1
){
  this.dout1=dout1;
  this.model1=model1;
  this.name=name;
  this.din1=din1;
  }
  public void run(){
  try{
  dout1.writeUTF(name);  // write the user name in output stream
  while(true){
  obj=new ObjectInputStream(din1);
  //read the list of user names
  alname=(ArrayList)obj.readObject()
  if(i>0)
  model1.clear()
  Iterator i1=alname.iterator();
  System.out.println(alname);
  while(i1.hasNext()){
  lname=(String)i1.next();
  i++;
 //add the user names in list box
  model1.addElement(lname);
  }
  }
  }catch(Exception oe){}
  }
}
//class is used to received the messages
class My implements Runnable{
  DataInputStream din;
  DefaultListModel model;
  My(DataInputStream din, DefaultListModel model){
  this.din=din;
  this.model=model;
  }
  public void run(){
  String str1="";
  while(true){
  try{
  str1=din.readUTF()// receive the message
  // add the message in list box
  model.addElement(str1);
  }catch(Exception e){}
  }
  }
}

 Download this application

Output of the Client Side Application is


  


 


  

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 28, 2008

Related Tutorials

Discuss: Client Side Application   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com