Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest 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

                         

» View all related tutorials
Related Tags: c com ide server application io client chat using cli this message id cast tar app cas connect start for

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

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

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 | Flex 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.