Mainstream Network Library Overview
Part I - .Net Networking Briefing
All .Net networking facilities are build on top of Winsock2 subsystem, so concepts such as non-blocking I/O, async I/O and I/O multiplexing can all be applied to the managed world. .Net network framework just wrapped Winsock2 and added some extra abstraction.
Most of the wrapping stuff is done through the System.Net.Sockets.Socket class.
If you are familiar with Winsock, you will feel comfortable with this
class. Here I will focus on the extra (OO style)abstraction that .Net
library team has added on top of this class.
There are 3 abstraction layers for .Net network programming:
-Raw Socket Layer
this model, you work directly with System.Net.Socket.Sockets class,
which is just a lightweight OO and PInvoke wrapper around native
Winsock2 functionalities. You can do anything in Winsock2 using methods
of this class. It's almost just method to method mapping. More info see .Net Network Programming Using Raw Socket.
- Tcp/Udp Layer
.Net network framework provides three extra classes: TcpClient, TcpListener, and UdpClient to ease
the task of Tcp/Udp application developing. They are built on top of
raw socket facilities and hide detail works such as connection
management, data buffering and parsing with the help of NetworkStream class. More info about Tcp/Udp model and more specific on .Net Tcp Programming, .Net Udp Programming
- Request/Response Layer
also known as pluggable protocol layer, in which all communication are
simple request/response pattern using standard URI to represent internet
resources. This abstraction is build on top of Raw Socket and Tcp/Udp
layer and it's also extensible so you can customize it. If you just need
to work in web world, this is a great facility. More info about programming pluggable protocols, FTP programming and HTTP programming
blocking/non-blocking/async model and the upper 3 layer concepts are
orthogonal and can be combined arbitrarily. For example, there are both
blocking TcpClient/TcpListener and async TcpClient/TcpListener.
Part II - Java Networking Briefing
Java has a totally different network library taxonomy.
1. Abstraction Layers
doesn't have real raw socket facility, which is similar to BSD socket
interface and Winsock2 interfaces. But it does contain class named as
Socket that is used do network communication. It can be roughly divided
into Tcp/Udp layer and URL layer.
- Tcp/Udp Layer
You deal with Tcp/Udp concepts directly when coding at this layer. Java provides 3 core classes:Socket/ServerSocket (Connection Oriented network facility, similar to .Net's TcpClient/TcpListener), DatagramSocket (Connectionless,
Message Oriented network facility, similar to .Net's UdpClient). These
classes also use I/O Stream to do reading/writing operations.
- URL Layer
to .Net's pluggable protocol layer, it uses URL to identify network
resources, provides convenient methods to talk to them. URL and URLConnection are those core classes.
2. Network I/O Model
For network I/O model, JDK provides blocking I/O in Java.Net name space since Java 1.0, multiplexing(a.k.a. non-blocking) I/O is available since Java 1.4 and Async I/O is added in Java 7, both in Java.Nio name space.
I/O consists of three core concepts: SelectableChannel, Selector and
SelectionKey.(illustrated below). Common practices using java nio
suggests that a network server is composed of acceptor to accept client connections, dispatcher to monitor network ready events and dispatch them to application handler, and application handler with worker thread pool, which handles application logic and generate response data to client.
I had write an time echo server using Java Nio and the upper architecture.
Java Asychronous IO(part of NIO.2)
is relatively new and not officially released yet. But its basic idea
is very simple: you issue the i/o request without blocking. The system
will notify you the completion of the request by meas of:
- calling callback function
- waiting handle to wait
- polling flag to poll
Part III - C/C++ Network Frameworks
1. LibEvent - http://monkey.org/~provos/libevent
Libevent is an event notification library not only for network
operations but for almost all types of i/o operations. The libevent API
provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached.
- It's essentially a Reactor design pattern, in which readiness event is triggered and notified.
2. APR - http://apr.apache.org/
APR provides platform independent interface for OS functionalites,
which includes some basic network i/o functions. But the interface is
rather basic, only blocking i/o is supported. You can see the list of functions from APR 1.4 Network I/O Doc.
3. BOOST.ASIO - http://think-async.com/
Boost ASIO is a scalable network framework using Proactor design pattern (which essentially uses completion notification mechanism, a.k.a Async Network I/O).
Here is in-depth document about Boost.Asio.
It's a heavyweight OO communication framework but it goes beyond just
computer proress communication. In a high level point view, it contains
- OS Adapter Layer:
it abstracts the underlying native OS's services using raw C wrappers
and provide a unified interface to upper layer components and
application developers. The OS services include:
．Concurrency and Synchronization
．File System I/O
- OO Facade Layer:
it wraps the OS adapter layer using OO, more specifically, the C++
language. Its functionalities are all the same as the adapter layer.
- Communication Framework Layer:
it integrates and enhances the lower-level C++ wrapper facades and
supports the dynamic configuration of concurrent distributed services
into applications. The framework the following components:
．Event Demultiplexing - The Reactor and Proactor are extensible, object-oriented demultiplexers . ．Service Initialization - The Acceptor and Connector components represents the active and passive initialization roles, respectively. ．Service Configuration - The Service Configurator supports the configuration of applications dynamically.
very heavy-weighted and contains many powerful components. It is also
the pioneer of concurrent and communication software architect. It
invents many famous design patterns in this area, for example, Reactor
5. ICE - http://www.zeroc.com/ice.html
ICE is a language independent RPC solution and supports Java/C#/C++/Python. It uses an IDL language called Slice(Specification Language for Ice) to define client/server interface.
The overall high level design of ICE can be found at: A New Approach to Object-Oriented Middleware