two threads having a common input reader

hello friends!!

in one of my projects i'm having two parallel threads running, each having a BufferedReader object. Each of these objects gets inputStream from a common socket.

the problem is, when something is input into the socket, only one of the threads reads that input at a time.. while i want both the threads to read that input..

to my surprise, 1st input is read by thread 1 n 2nd by thread 2, 3rd by thread 1 and so on...

i would b plsd if any suggestions r posted in this regard..

thanx

[535 byte] By [manishmulani@nitka] at [2007-11-27 7:03:32]
# 1
Don't be surprised. That's the way it's supposed to work. Also if you have two buffers, they are going to fill and therefore steal data from each other. If you want both threads to read the same data you'll have to replicate it somewhere yourself.Why do you want to do this?
ejpa at 2007-7-12 18:54:46 > top of Java-index,Core,Core APIs...
# 2

thanx ejp for replying..

actually i'm trying a group chat kinda thing...

in which i've a list of users logged in for each client.

in that i'm trying to implement "send private messages" b/w any 2 users

i've a different server running for private messages which will recv d reqst from one client to connect to a particular client so that it can send priv msg. this will b taken care by corresponding sockets running at server side

that is whenever one client wants to connect to another client for private chat

server is made to know the source n destination sockets , it starts a thread for that particular pair of sockets for communication

client1->socket1 (at server) -> socket2 (at server) -> client2 and vice versa

is this idea correct?

manishmulani@nitka at 2007-7-12 18:54:46 > top of Java-index,Core,Core APIs...
# 3

clarification:

server has 'n' sockets open ,,, out of which it identifies 2 sockets which want to communicate n starts a thread for that pair of sockets ( for communication )

for every socket at server side., there's initially a single thread running which has a BufferedReader object ( meant for reading frm socket ) say in

1.

socket me

while((input=readLine())!=null)

{

when connection established frm client xyz, identify the socket msgFrm

start a thread T for communication ( passing 2 sockets me and msgFrm )

}

2.

Thread T

this thread has the same BufferedReader object created ( i.e for the same socket me

this BufferedReader object reads the msg from client xyz ( i.e frm socket msgFrm )

the problem is both the threads have BufferedReader object in

in=new BufferedReader(new InputStreamReader(me.getInputStream()));

some of the msgs have to b read by first bufferedReader object and others by the second object

how do i achieve this?

manishmulani@nitka at 2007-7-12 18:54:46 > top of Java-index,Core,Core APIs...
# 4

> Don't be surprised. That's the way it's supposed to

> work. Also if you have two buffers, they are going to

> fill and therefore steal data from each other. If you

> want both threads to read the same data you'll have

> to replicate it somewhere yourself.

pls tell me how i should replicate it...

manishmulani@nitka at 2007-7-12 18:54:46 > top of Java-index,Core,Core APIs...