Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Creating a multi-user TCP server

Reply
Thread Tools

Creating a multi-user TCP server

 
 
jamesgoode
Guest
Posts: n/a
 
      04-04-2008
Hi,

I'm trying to create a multi-user TCP server, without the use of a
thread-per-connection model. My plan is to create an instance of the
ClientConnection class, which is stored in an array, for each
connection. A 'connection listener' thread handles this. The inputs
from the clients are then handled in a loop, which looks like this:

while (true)
{

for (int i = 0; i < MAX_CONNECTIONS; i++)
{
try {
if (connections[i]!=null)
if (connections[i].in.ready())
connections[i].out.writeBytes(connections[i].in.readLine()+"\n
\r");
} catch (IOException e) {
// TODO Auto-generated catch block
connections[i] = null;
}
}

}

(connections[i].in is a BufferedReader)

The problem is, that BufferedReader.ready() always seems to return
true, and that BufferedReader.readLine() halts the program until there
is a line to read.

All help is greatly appreciated.

Many thanks,

--James.
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      04-04-2008
jamesgoode wrote:
> Hi,
>
> I'm trying to create a multi-user TCP server, without the use of a
> thread-per-connection model. My plan is to create an instance of the
> ClientConnection class, which is stored in an array, for each
> connection. A 'connection listener' thread handles this. The inputs
> from the clients are then handled in a loop, which looks like this:
>
> while (true)
> {
>
> for (int i = 0; i < MAX_CONNECTIONS; i++)
> {
> try {
> if (connections[i]!=null)
> if (connections[i].in.ready())
> connections[i].out.writeBytes(connections[i].in.readLine()+"\n
> \r");
> } catch (IOException e) {
> // TODO Auto-generated catch block
> connections[i] = null;
> }
> }
>
> }
>
> (connections[i].in is a BufferedReader)
>
> The problem is, that BufferedReader.ready() always seems to return
> true, and that BufferedReader.readLine() halts the program until there
> is a line to read.
>
> All help is greatly appreciated.
>
> Many thanks,
>
> --James.


BufferedReader.readLine() blocks until a line terminator is found in the
stream. That is going to be inconvenient for what you have planned. I
think it would be possible to check BufferedReader.ready() in a loop and
read a single char from the stream until ready() returns false, then
move on to your next stream. I don't see anything in the docs that
suggest that ready() will always return true like
InputStream.available() does. One advantage to this method is that it
should scale well with a ThreadPoolExecutor to a lot of connections.

Depending on what sort of latency you can tolerate you might be able to
just block on a read and set a short timeout. Or the other option is to
use NIO and its non-blocking features.

Are you sure that you are going to have so many simultaneous connections
that the thread per connection will be a problem? For a small number of
connections (less than 50 on a Windows machine) it is very easy to code.

--

Knute Johnson
email s/nospam/linux/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDem
 
Reply With Quote
 
 
 
 
jamesgoode
Guest
Posts: n/a
 
      04-05-2008
On Apr 5, 12:34 am, Knute Johnson <(E-Mail Removed)>
wrote:
> jamesgoode wrote:
> > Hi,

>
> > I'm trying to create a multi-user TCP server, without the use of a
> > thread-per-connection model. My plan is to create an instance of the
> > ClientConnection class, which is stored in an array, for each
> > connection. A 'connection listener' thread handles this. The inputs
> > from the clients are then handled in a loop, which looks like this:

>
> > while (true)
> > {

>
> > for (int i = 0; i < MAX_CONNECTIONS; i++)
> > {
> > try {
> > if (connections[i]!=null)
> > if (connections[i].in.ready())
> > connections[i].out.writeBytes(connections[i].in.readLine()+"\n
> > \r");
> > } catch (IOException e) {
> > // TODO Auto-generated catch block
> > connections[i] = null;
> > }
> > }

>
> > }

>
> > (connections[i].in is a BufferedReader)

>
> > The problem is, that BufferedReader.ready() always seems to return
> > true, and that BufferedReader.readLine() halts the program until there
> > is a line to read.

>
> > All help is greatly appreciated.

>
> > Many thanks,

>
> > --James.

>
> BufferedReader.readLine() blocks until a line terminator is found in the
> stream. That is going to be inconvenient for what you have planned. I
> think it would be possible to check BufferedReader.ready() in a loop and
> read a single char from the stream until ready() returns false, then
> move on to your next stream. I don't see anything in the docs that
> suggest that ready() will always return true like
> InputStream.available() does. One advantage to this method is that it
> should scale well with a ThreadPoolExecutor to a lot of connections.
>
> Depending on what sort of latency you can tolerate you might be able to
> just block on a read and set a short timeout. Or the other option is to
> use NIO and its non-blocking features.
>
> Are you sure that you are going to have so many simultaneous connections
> that the thread per connection will be a problem? For a small number of
> connections (less than 50 on a Windows machine) it is very easy to code.
>
> --
>
> Knute Johnson
> email s/nospam/linux/
>
> --
> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
> ------->>>>>>http://www.NewsDem


Thanks for your help,

I'll try what you've suggested, and incorporate it into the
ClientConnection class (just to make it easier).

I'll post back soon with results, and anything else that I'll have
found.

--James.
 
Reply With Quote
 
Christian
Guest
Posts: n/a
 
      04-05-2008
jamesgoode schrieb:
> Hi,
>
> I'm trying to create a multi-user TCP server, without the use of a
> thread-per-connection model.


I would recommend you to look a bit more into non Blocking IO .. there
is no way around this if you really don't want one Thread per Connection .

So you are gonna need to use a selector. And for Transforming bytes to
chars you have to use a Char(En/De)Coder.

For starting I found that rather intresting:
http://www.exampledepot.com/egs/java...ver.html?l=rel

also the links on the page help with the other stuff involved in non
blocking IO.


Christian
 
Reply With Quote
 
EJP
Guest
Posts: n/a
 
      04-06-2008
Knute Johnson wrote:
> jamesgoode wrote:
>> The problem is, that BufferedReader.ready() always seems to return
>> true, and that BufferedReader.readLine() halts the program until there
>> is a line to read.

>
> BufferedReader.readLine() blocks until a line terminator is found in the
> stream. That is going to be inconvenient for what you have planned. I
> think it would be possible to check BufferedReader.ready() in a loop and
> read a single char from the stream until ready() returns false, then
> move on to your next stream. I don't see anything in the docs that
> suggest that ready() will always return true like
> InputStream.available() does.


Err, it doesn't return 'true', it returns an integer >= 0, which can
always be zero according to the contract, and always is e.g. in the case
of an SSLSocket. All that 'ready()' does ultimately is to return 'true'
if there is buffered data in the BufferedReader or available() > 0.

However there is no reason why ready() == true should mean there is an
entire line waiting to be read. So your readLine() can block.

So I would either overcome your phobia about threads, which are OK in
units of hundreds or so, or use NIO if you know you are going to have
tens or hundreds of thousands of connections. Mostly the blocking
threads arising out of normal use of java.net are doing nothing except
block.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
send tcp raw socket (bogus tcp header length) Tiger C Programming 5 05-01-2006 05:53 AM
tcp/ip vs microsoft tcp/ip ver 6 john Computer Support 1 08-05-2005 02:45 PM
NAT two outside TCP ports to one inside TCP port Kevin Cisco 1 11-10-2004 08:15 AM
Secure and compressed TCP/IP connection (for any TCP/IP services and applications) Pavel Aronovich Computer Security 0 02-22-2004 08:35 AM
LPR/Standard, TCP/IP, HP TCP/IP Ports DJ Chiro MCSE 1 11-07-2003 08:06 PM



Advertisments