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");

}

}

}

[5114 byte] By [kothari_neerava] at [2007-11-27 8:29:49]
# 1
> its not heppeningWhat is happening?You should break out of your infinite loop if you read a null from readLine(), and close and remove the socket's PrintWriter from the vector too.
ejpa at 2007-7-12 20:20:12 > top of Java-index,Core,Core APIs...
# 2

hi.

if the loop is to end after null, the subsequent messages from the client are not read. this code is working fine as far as 1 client is concerned.

what was not happening was that when client sent a message it was not being received by all pther cleints. anyway, i solved the problem with this for loop instead of the one i wrote.

for(int j=0; j<i; j++)

{

PrintWriter o = (PrintWriter) v.elementAt(j);

o.println(str);

}

now there is some client side tweaking remaining.

Message was edited by:

kothari_neerav>

kothari_neerava at 2007-7-12 20:20:12 > top of Java-index,Core,Core APIs...
# 3

> if the loop is to end after null, the subsequent

> messages from the client are not read.

If you get a null from readLine() it means there aren't any more messages from the sender. The null means that the sender has closed its end of the connection. That's why you should stop reading, close the socket, etc. Otherwise once you get this condition your loop will spin forever.

There is no logical difference between your old 'for' loop and your new one. You must have fixed something else too.

ejpa at 2007-7-12 20:20:12 > top of Java-index,Core,Core APIs...