tags:

views:

55

answers:

4

Hi all,

I had built a java TCPServer using serversocketchannels running on one port. However, it is not very scalable as it attends to one incoming socket (blocking mode) only.

I want to extend this TCPServer to service multiple incoming sockets (maximum 10 incoming sockets). As such, am wondering if i should implement the TCPServer using non-blocking io or use thread+blocking io.

A: 

Paul Tyma recently compared the two approaches, generating diverse discussion. Under certain circumstances, modern threading libraries can outperform java.nio.channels.Selector. As the result is somewhat counter-intuitive, you may have to prototype both to get a definitive answer.

trashgod
I agree totally. Prototyping is definitely needed to get a definitive answer. I was working with InputStream earlier with one incoming socket. I was quite perplexed to find out InputStream somehow didn't work properly. I would get missing incoming data, repeated incoming data. Then I moved to channels and viola! It's all good.
Yakult121
That can only have been coding errors in your original code. TCP doesn't lose data, and neither do InputStreams. Otherwise for example this forum wouldn't work.
EJP
Hmm... I implemented my pseudocode to both java and c#. C# took in the incoming data perfectly, but it was not the same when using InputStream with java. Java (with InputStream) did not receive all the incoming data and in some cases, had received repeated data. Java (with channels) worked fine.
Yakult121
@Yakult121: Like @EJP, I have to wonder about a coding error, perhaps related to synchronization. Moreover, it sounds like you are well acquainted with channels in other environments, so your Java implementation may have been more robust.
trashgod
@trashgod - I'm just a newbie writing java channel apps and such. I've been trying to replicate Java channels on C# but i don't find any similar c# channels implementation.Anyhoo, I'm looking into Apache MINA.
Yakult121
A: 

JBoss-Netty or Apache-Mina are nio framework that provide much things to implement your own server. So, now i'm using netty and happy with it.

secmask
Thanks secmask! I'm also looking at Apache Mina. I was hoping to exhaust all leads on the existing Java classes before looking at Apache-Mina.
Yakult121
A: 

With only 10 incoming sockets I don't think the effect is clear to see. What you should do is to focus on the upper layer (protocol, application) and leave that low level network implementation to a framework. I would recommend the Apache Mina for that job. As you will see, I does the job very well with blocking or non blocking, you choose; and leave open interfaces for you to implement the protocol & application.

instcode
I'll work on Apache Mina then. Thanks instcode!
Yakult121
A: 

I would use threads and blocking I/O until you know you have at least 1000 concurrent connections. That also gives you an easy way to get it working. When and if you get to 1000, evaluate.

EJP
I'll do that. :P Thanks EJP!
Yakult121