469,331 Members | 6,372 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

listen to multiple sockets

hello,

this subject has been discussed many times in here, but a solution was
not found as far as could get to know. but perhaps newer developments of
java made one possible that i dont know of.

the problem is how i can read from multiple sockets without having to
create a new thread for each, which, i guess, affects the programms
performance if the number of connections to a server is very high.
would it practicable to use a thread that loops throug all open connections
and tests with the available() method if there is data that could be
read and then a worker thread can handle the rest? i dont know how effec-
tive the available method ist working, but i dont really think that this
is a serios possibility, is it?
so, can anyone give me a hint how to do this best?

max junker
Jul 17 '05 #1
3 7264
I am no java expert, but available may use the poll() system call at the
native interface to your socket. If it does you should be able to do so,
safely. Also, the new JDK has async i/o - which may also help.

-Misk

max junker wrote:
hello,

this subject has been discussed many times in here, but a solution was
not found as far as could get to know. but perhaps newer developments of
java made one possible that i dont know of.

the problem is how i can read from multiple sockets without having to
create a new thread for each, which, i guess, affects the programms
performance if the number of connections to a server is very high.
would it practicable to use a thread that loops throug all open connections
and tests with the available() method if there is data that could be
read and then a worker thread can handle the rest? i dont know how effec-
tive the available method ist working, but i dont really think that this
is a serios possibility, is it?
so, can anyone give me a hint how to do this best?

max junker


Jul 17 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

max junker wrote:

[snip]
the problem is how i can read from multiple sockets without having
to create a new thread for each, which, i guess, affects the

[snip]

Hi,
One word: SocketChannels. When you create a SocketChannel for each
connection, you can register each one with a Selector. Then, one
thread just calls Selector.select(), and then handles everything that
there is to be handled on all connections. You can even use the same
Selector with ServerSocketChannels.

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/YfJMwxczzJRavJYRAovPAKDs0M2Kxw1d5DNlrmR0CQNVny8GyQ CffHkg
InRMVWO9mFX+cC5kChGW/yw=
=5hq3
-----END PGP SIGNATURE-----
Jul 17 '05 #3
thank you very much!
Thats exactly what i was looking for!
obviously it`s sometimes worth to check new features
in the API!

max junker

Chris <ch*******@hotmail.com> wrote in message news:<Zws8b.70$Cu3.12@edtnps84>...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

max junker wrote:

[snip]
the problem is how i can read from multiple sockets without having
to create a new thread for each, which, i guess, affects the

[snip]

Hi,
One word: SocketChannels. When you create a SocketChannel for each
connection, you can register each one with a Selector. Then, one
thread just calls Selector.select(), and then handles everything that
there is to be handled on all connections. You can even use the same
Selector with ServerSocketChannels.

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/YfJMwxczzJRavJYRAovPAKDs0M2Kxw1d5DNlrmR0CQNVny8GyQ CffHkg
InRMVWO9mFX+cC5kChGW/yw=
=5hq3
-----END PGP SIGNATURE-----

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Gabriele Farina | last post: by
2 posts views Thread by Paul A. Steckler | last post: by
11 posts views Thread by Bonj | last post: by
reply views Thread by Saurabh Kumar | last post: by
3 posts views Thread by derSchweiz | last post: by
reply views Thread by =?Utf-8?B?U3RldmVT?= | last post: by
4 posts views Thread by casybay | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.