Hello,
Can anyone pls help me out with this code, its the server portion of the client server program..i have downloaded it from net

Socket s,s1,s2;
ArrayList al=new ArrayList(); 
ArrayList al1=new ArrayList();
ArrayList al2=new ArrayList();
ArrayList alname=new ArrayList();
        
	MyServer()throws IOException{ 
	ServerSocket ss=new ServerSocket(10004);	
		while(true){
			s=ss.accept();	
			s1=ss.accept();
			s2=ss.accept();
			al.add(s);	
			al1.add(s1);
			al2.add(s2);
			System.out.println("Client is Connected");
			MyThread2 m=new MyThread2(s2,al2,alname); 
			Thread t2=new Thread(m);
			t2.start();

			MyThread r=new MyThread(s,al);
			Thread t=new Thread(r);
			t.start();
			
			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);
		MyThread2.alname.remove(sname);
		MyThread2.every();
		al1.remove(s1);
		MyThread.al.remove(s);
		MyThread2.al2.remove(s2);
		if(al1.isEmpty())
			System.exit(0); 
		}catch(Exception ie){}
	}
}


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());	
		every();
		}catch(Exception oe){System.out.println("Main expression"+oe);}
	}
	
	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); 
			dout1.flush();
			obj.flush();
		}
	}
}

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(); 
					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();
		}
	}
}

I just want to know that why are we using 3 sockets for client and not 1, and why 4 ArrayLists, What is each of the ArrayLists doing.

and if i have a logon frame, then how can i call the client form if i click on the login button on the login frame.

please help me out friends...
I am trying to sort all these things myself, but i am not able to sort it properly...
Thanks.

dantinkakkar commented: It's better to write your own code, after all... -1

I just want to know that why are we using 3 sockets for client and not 1, and why 4 ArrayLists,

I don't know where you got this code from but to me it looks very amateur - maybe a beginner's first attempt at a socket program? Unless there's some very subtle reason (in which case where are the comments?) those replicated variables and methods just look like bad programming to me. You will learn nothing from this except how not to write Java.
I recommend that you throw away this junk and write your own decent code using the Oracle Java tutorials to guide you
http://docs.oracle.com/javase/tutorial/networking/sockets/
... and you can get help here on Daniweb ;)

Whoa dude. You expect me or anyone else for that matter to go through this? And you've not even placed any comments! I can help you at one place, though --

An ArrayList is a sort of list of elements in which you don't need to define it's size. You can keep adding elements using a func(). It's just an easier to build up and more flexible version of a typical array, and it falls under the java.util package. Next time you want someone to examine your code, please place comments. I'm not going through that.

Whoa dude. You expect me or anyone else for that matter to go through this? And you've not even placed any comments!

he didn't place comments because, as he said himself:
it's not his code. it's some code he has "downloaded it from net"

:\ No Wonder he had to ask for help, then. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.