Java – Choosing Between Socket and SocketChannel

javaserversockets

I am designing a server for a remote file store in Java and trying to decide whether to use asynchronous java.nio.SocketChannels or the older blocking model with Sockets. What kind of factors should I consider when choosing between these two server architectures and what kinds of tasks would be better suited to one versus the other?

Specifically, I am wondering how these respective approaches compare as:

  1. the number of connected clients scale up,
  2. the length of a client connection increases, and
  3. the amount of data transferred increases.

Also, are there any other factors that I should be aware of that would favor one approach over the other?

Best Answer

Why are you trying to solve an already solved problem? There already exists at least two good ones:

Grizzly

Writing scalable server applications in the Java™ programming language has always been difficult. Before the advent of the Java New I/O API (NIO), thread management issues made it impossible for a server to scale to thousands of users. The Grizzly NIO framework has been designed to help developers to take advantage of the Java™ NIO API. Grizzly’s goal is to help developers to build scalable and robust servers using NIO as well as offering extended framework components: Web Framework (HTTP/S), WebSocket, Comet, and more!

Netty

Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server.

'Quick and easy' doesn't mean that a resulting application will suffer from a maintainability or a performance issue. Netty has been designed carefully with the experiences earned from the implementation of a lot of protocols such as FTP, SMTP, HTTP, and various binary and text-based legacy protocols. As a result, Netty has succeeded to find a way to achieve ease of development, performance, stability, and flexibility without a compromise.

It sounds to me like you are less interested in solving the problem yourself than you are in just having the problem be solved - so you can have your remote file store. Personally, I use Netty in my application and it works great. Don't reinvent the wheel - use someone else's.

Related Topic