I’m developing a server that has to be able to cope with several (hundreds) clients simultainiousely. Each connection is 100% autonomous (hard word to spell) and there are no “shared resources”.
My first, basic implementation was a straight-forward approach following the guidlines from sun’s tutorials.
The following code is executed in a separate thread, serving a certain port. I have totally 5 different ports running on the serve, hence 5 threads each serving their individual ports;
ServerSocketChannel ssc = ServerSocketChannel.open();
InetSocketAddress isa = new InetSocketAddress(port);
ssc.socket().bind(isa);
while(true)
{
SocketChannel sc = ssc.accept();
if (sc != null)
{
Service serviceInstance = (Service)service.newInstance();
serviceInstance.setSocketChannel(sc);
serviceInstance.start();
}
}
Service refers to my own abstract class, designed to deal with this typical thread’s (port’s) behaivour.
So far, so good but since I wanted several clients to be able to connect to the server in parallell (if there is such a thing…), I modified my “serviceInstance” to start a seperate thread to handle its client.
Now this is where it starts to get boring - Suddenly my the SocketChannel closes (stocastically - sometimes it work, sometimes it throws an exception since it closed and I try to read / write to / from it)
Can this be (it seems to but I ask just to get you advice & tips’n’tricks) due to the fact that I use threads? Why does the channel suddenly drop dead?
I think that what I really am looking for in this thread is Your thought of a better approach to solve my problem;
Designing a Java server that has 5 different port open for connections with 5 completely different behaivours, built to cope with at least a couple of hundre clients…
I feel that I’m having trouble to describe my problem correctly, but I hope you understand my situation - sort of
Thanx / Markus