vector trouble.
hi. i am making a messenger program. in the server side i have created a vector which is supposed to store all the Printwriter objects in it and then after each message sent by the client it is supposed to iterate through the vector objects and send all the clients the messengs. like in a chat room. however its not heppening. here is the code...
//a server that can receive and send data to client.
package server;
import java.io.*;
import java.net.*;
class Server
{
publicstaticvoid main(String[] args)
{
try
{
ServerSocket ss =new ServerSocket(9090);//Opens a new ServerSocket.
while(true)//Listens endlessly for a socket connection
{
Socket sc = ss.accept();// returns a socket object when connection is requested by the client.
//Creates a new SocketHandler object, passes the referrence of the socket object as an argument to its constructor and dispatches a new thread to work on it.
new Thread(new SocketHandler(sc)).start();
}
}
catch(IOException e)
{
System.out.println("IOException. Cannot initialize server");
}
}
}
package server;
import java.io.*;
import java.net.*;
import java.util.Enumeration;
import java.util.Vector;
class SocketHandlerimplements Runnable
{
Socket soc;// handle to refer a socket object.
//variable to count number of socket objects created depenging on number of time constructor invoked
staticint i =0;
BufferedReader in;
PrintWriter out;
static Vector v =new Vector();
SocketHandler(Socket sc)//constructor to accept handle to a socket object.
{
soc = sc;//assign the handle of socket object to instance variable soc.
i++;//increments socket count.
//System.out.println(i);//display the number of the socket connection.
}
publicvoid run()
{
try
{
//define in object to accept string.
in =new BufferedReader(new InputStreamReader(soc.getInputStream()));
//define out object to send a string.
out =new PrintWriter(new BufferedWriter(new OutputStreamWriter(soc.getOutputStream())),true);
v.addElement(out);
while(true)//runs as long as client doesnot close the connection.
{
String str = in.readLine();//receive message from client.
System.out.println(str);
for(Enumeration e = v.elements(); e.hasMoreElements(); )
{
PrintWriter o = (PrintWriter) e.nextElement();
o.println(str);
}
}
}
catch(IOException e)
{
System.out.println("IO Exception");
}
}
}

