Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
Tutorials
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
Server Side Application
Server side application is used to get the message from any client and broadcast to each and every client. And this application is also used to maintain the list of users and broadcast this list to everyone.
 
 

Server Side Application

                         

Server side application is used to get the message from any client and broadcast to each and every client. And this application is also used to maintain the list of users and broadcast this list to everyone.

The Server side application follows these steps

  • Firstly creates a new server socket by the ServerSocket ss = new ServerSocket(1004);
           
  • After creating the ServerSocket it accepts the client socket and add this socket into arraylist.
            Socket s = ss.accept();
            ArrayList al2 = new ArrayList();
            Al2.add(s);

          
  • After getting the client socket it creates a thread and make DataInputStream for this socket. After creating the input stream its read the user name and add it to arraylist and this arraylist object write in ObjectOutputStream of each client by using iterator. 
            DataInputStream din1=new DataInputStream(s.getInputStream)
            ArrayList alname=new ArrayList();
            alname.add(din1.readUTF());
        
            Iterator i1=al2.iterator();
            Socket st1; 
            while(i1.hasNext()){
            st1=(Socket)i1.next();
            dout1=new DataOutputStream(st1.getOutputStream());
            ObjectOutputStream obj=new ObjectOutputStream(dout1);
            obj.writeObject(alname);
            }

            
  • After this it makes a new thread and makes one DataInputStream for reading the messages which sends by the client and after reading the message its creates the DataOutputStream for each socket and writes this message in each client output stream through iterator.
            String str=din.readUTF();
            Iterator i=al.iterator();
            Socket st;
            while(i.hasNext()){
            st=(Socket)i.next();
            dout=new DataOutputStream(st.getOutputStream());
            dout.writeUTF(str);
            dout.flush();
            }

           
  • If any client Logged out then server received the client name and server remove it from the arraylist. And sends this updated arraylist to all client.
            sname=ddin.readUTF();
            alname.remove(sname);

Here is the code of the Server side application :

/****************************************************************
*  Version    :  1.0
*  Date       :  02/03/2007
*  
*  Description
*  This is a Server Side application of Chat System.
*  This application is used for receiving the messages from any client
*  and send to each and every client and in this we can maintain the
*  list of all online users.
*
*  Remarks
*  This application is unable to provide the private chatting facility
******************************************************************/

import java.io.*;
import java.net.*;
import java.util.*;

public class MyServer{
  ServerSocket ss;
  Socket s;
  ArrayList al=new ArrayList();
  ArrayList al1=new ArrayList();
  ArrayList al2=new ArrayList();
  ArrayList alname=new ArrayList();
  Socket s1,s2;
  MyServer()throws IOException{
    ss=new ServerSocket(1004);  // create server socket
    while(true){
      s=ss.accept();  //accept the client socket
      s1=ss.accept();
      s2=ss.accept();
      al.add(s);  // add the client socket in arraylist
      al1.add(s1);
      al2.add(s2);
      System.out.println("Client is Connected");
//new thread for maintaning the list of user name
      MyThread2 m=new MyThread2(s2,al2,alname) 
            
      Thread t2=new Thread(m);
      t2.start();
//new thread for receive
and sending the messages
      MyThread r=new MyThread(s,al);
      Thread t=new Thread(r);
      t.start();
      // new thread for
update the list of user name
      MyThread1 my=new MyThread1(s1,al1,s,s2) 
               
      Thread t1=new Thread(my);
      t1.start();
    }
  }
  public static void main(String[] args){
    try{
      new MyServer();      
    }catch (IOException e){}
  }
}
//class is used to update the list of user name
class MyThread1 implements Runnable{
  Socket s1,s,s2;
  static ArrayList al1;
  DataInputStream ddin;
  String sname;
  MyThread1(Socket s1,ArrayList al1,Socket s,Socket s2){
    this.s1=s1;
    this.al1=al1;
    this.s=s;
    this.s2=s2;
  }
  public void run(){  
    try{
    ddin=new DataInputStream(s1.getInputStream());
    while(true){
    sname=ddin.readUTF();
    System.out.println("Exit  :"+sname);
//remove the logout user
name from arraylist
    MyThread2.alname.remove(sname);
 
    MyThread2.every();
    al1.remove(s1);
    MyThread.al.remove(s);
    MyThread2.al2.remove(s2);
    if(al1.isEmpty())
    System.exit(0)//all client has been logout
    }
    }catch(Exception ie){}
  }
}

// class is used to maintain the list of all online users
class MyThread2 implements Runnable{
  Socket s2;
  static ArrayList al2;
  static ArrayList alname;
  static DataInputStream din1;  
  static DataOutputStream dout1;

  MyThread2(Socket s2,ArrayList al2,ArrayList alname){
    this.s2=s2;
    this.al2=al2;
    this.alname=alname;
  }
  public void run(){
    try{
    din1= new DataInputStream(s2.getInputStream());
// store the user name in arraylist
    alname.add(din1.readUTF());
    every();
    }catch(Exception oe){}
  }
  // send the list of user name to all client
  static void every()throws Exception{
    Iterator i1=al2.iterator();
    Socket st1;    

    while(i1.hasNext()){
      st1=(Socket)i1.next();
      dout1=new DataOutputStream(st1.getOutputStream());
      ObjectOutputStream obj=new ObjectOutputStream(dout1);
//write the list of users
in stream of all clients
      obj.writeObject(alname)
      dout1.flush();
      obj.flush();
    }
  }
}
//class is used to receive the message and 
//send it to all clients
class MyThread implements Runnable{
  Socket s;
  static ArrayList al;
  DataInputStream din;
  DataOutputStream dout;

  MyThread(Socket s, ArrayList al){
    this.s=s;
    this.al=al;
  }
  public void run(){
    String str;
    int i=1;
    try{
    din=new DataInputStream(s.getInputStream());
    }catch(Exception e){}
    
    while(i==1){
        try{
          
          str=din.readUTF()//read the message
          distribute(str);
        }catch (IOException e){}
      }
  }
  // send it to all clients
  public void distribute(String str)throws IOException{
    Iterator i=al.iterator();
    Socket st;
    while(i.hasNext()){
      st=(Socket)i.next();
      dout=new DataOutputStream(st.getOutputStream());
      dout.writeUTF(str);
      dout.flush();
    }
  }
}

Download this application

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

Very Good

Posted by naveen on Tuesday, 12.18.07 @ 14:39pm | #42817

nice application.helpful

Posted by sandeep on Monday, 11.5.07 @ 14:54pm | #35586

Latest Tutorials:
J2ME Event Handling Ex
J2ME HashTable Example
J2ME Icon MIDlet Examp
J2ME Image Item Exampl
J2ME Image Example
J2ME Item State Listen
J2ME Key Codes Example
J2ME KeyEvent Example
J2ME Label Example
J2ME Random Number
J2ME Read File
J2ME RMS Sorting Examp
J2ME Timer MIDlet Exam
Custom Item in J2ME
Creational Design Patt
Design Patterns
Throwing Run time exce
Grid in Echo3
Creating Table in Echo
JPA Introduction
Java bigdecimal toBigI
Java bigdecimal shortV
Java bigdecimal shortV
Java bigdecimal signum
Java bigdecimal stripT
Java bigdecimal subtra
Java bigdecimal subtra
Java bigdecimal toBigI
Java bigdecimal toEngi
Java bigdecimal toPlai
Java bigdecimal toStri
Java bigdecimal ulp ex
Java bigdecimal unscal
Java bigdecimal valueO
Java bigdecimal valueO
Java bigdecimal valueO
Java bigdecimal setSca
Java bigdecimal setSca
Java bigdecimal scaleB
Java bigdecimal scale
Java bigdecimal round
Java bigdecimal remain
Java bigdecimal remain
Java bigdecimal precis
Java bigdecimal pow me
Java bigdecimal pow ex
Java bigdecimal plus m
Java bigdecimal plus e
Java bigdecimal negate
Java bigdecimal negate
Java bigdecimal multip
Java bigdecimal multip
Java BigDecimal movePo
Java bigdecimal movePo
Java bigdecimal min ex
Java bigdecimal max ex
CheckBox component in
Visibility of Componen
Loading delay componen
Simple input applicati
Opening a new window i
Hello World in Echo3 f
Use of Local Inner cla
JSP bean set property
Java Method Synchroniz
Java Method Return Val
JAVA Method Wait
JDBC vs ORM
Java BigDecimal divide
Java BigDecimal divide
Java BigDecimal divide
Java BigDecimal divide
Java BigDecimal divide
Java BigDecimal double
Java BigDecimal equals
Java BigDecimal hashCo
Java BigDecimal intVal
Java BigDecimal intVal
Java BigDecimal longVa
Java BigDecimal longVa
Java BigDecimal abs ex
Java BigDecimal add ex
Java BigDecimal byteVa
Java Bigdecimal class
Java BigInteger long
Java BigDecimal compar
Java divide method exa
Java BigDecimal floatV
Appending Image into t
J2ME Convert Date To S
Appending string in J2
J2ME Enumeration Examp
J2ME Display Size Exam
J2ME Current Date And
J2ME Form Class
Creating Dynamic Tree
Introduction to RCFace
JSP bean get property
Java bean example in J
J2ME Record Store Exam
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.