Results 1 to 2 of 2

Thread: java server

  1. #1
    mayanksachan is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default java server

    i m facing a problem in my server that is when we logout our client window then further those client window does not connect to the server.plz help
    MyServer.java is given below:

    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");
    MyThread2 m=new MyThread2(s2,al2,alname); //new thread for maintaning the list of user name
    Thread t2=new Thread(m);
    t2.start();

    MyThread r=new MyThread(s,al);//new thread for receive and sending the messages
    Thread t=new Thread(r);
    t.start();

    MyThread1 my=new MyThread1(s1,al1,s,s2); // new thread for update the list of user name
    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);
    MyThread2.alname.remove(sname);//remove the logout user name from arraylist
    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());
    alname.add(din1.readUTF()); // store the user name in arraylist
    every();
    }catch(Exception oe){System.out.println("Main expression"+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);
    obj.writeObject(alname); //write the list of users in stream of all clients
    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();
    }
    }
    }

  2. #2
    Horcrux7 is offline Member
    Join Date
    Sep 2010
    Posts
    33
    Rep Power
    0

    Default

    It look like that your client create 3 sockets to the same server port. The 3 sockets have different behavior. This is a very bad design. The order is not define. If there are 2 clients connect at the same time then there can be chaos.

    What do you means with "does not connect to the server"? DO you receive a exception (which)? Or does it hang? Then create a thread dump.
    Volker Berlin
    www.inetsoftware.de

Similar Threads

  1. java server and c client ?????????
    By biebo in forum Networking
    Replies: 7
    Last Post: 07-24-2010, 04:35 AM
  2. Java , Java script , Java server pages
    By DoNot in forum New To Java
    Replies: 5
    Last Post: 06-15-2010, 09:44 AM
  3. Add Java Server Face Fragment in JSP with JAVA
    By fabio.beoni in forum New To Java
    Replies: 0
    Last Post: 02-05-2009, 12:14 PM
  4. java mail server
    By krismedia in forum New To Java
    Replies: 2
    Last Post: 01-23-2008, 04:41 AM
  5. Help with IRC server in java
    By mathias in forum Networking
    Replies: 1
    Last Post: 08-07-2007, 06:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •