Search Wiki:
Note: Now Hosted on CodePlex

Version 1.4 and newer are hosted on CodePlex.

Resource Page Description
Makes use of the Nito.Async library to demostrate client and server TCP/IP sockets capable of sending and receiving objects as well as detecting dropped connections.

Screenshots

The sample server:
Nito.Sockets.Server.png

The sample client:
Nito.Sockets.Client.png

More on Nito.Async.Sockets

There are three main problems faced by newcomers to TCP/IP:

  1. Thread synchronization for asynchronous sockets. Most of the TCP/IP functions are designed for maximum performance, leaving the necessary synchronization up to the end users.
  2. Non-packetized data. Most people read about how TCP/IP uses packets, and they believe that message boundaries are preserved. Local-machine testing confirms this misunderstanding. However, TCP/IP sockets deal with a stream of bytes, not messages.
  3. Detection of dropped connections. Again, people read about TCP/IP's keepalive packets, but don't realize that they are pretty much useless.

The ClientTcpSocket, ServerTcpSocket, and ServerChildTcpSocket deal with problem (1) by implementing the event-based asynchronous pattern.
The SimpleClientTcpSocket, SimpleServerTcpSocket, and SimpleServerChildTcpSocket handle all three problems by implementing the event-based asynchronous pattern and defining a protocol with message boundaries and automatic keepalive messages.

The binaries for Nito.Async.Sockets (and Nito.Async) are included in the release of this sample code. They may also be downloaded (with source) from http://www.codeplex.com/NitoAsync.

Socket example code

The "LowerLevel" example code uses ClientTcpSocket and ServerTcpSocket for thread synchronization, uses Nito.Async.Sockets.SocketPacketProtocol to define a protocol with message boundaries, and uses a timer to send keepalive messages.

The "Simple" example code uses SimpleClientTcpSocket and SimpleServerTcpSocket to provide the same functionality with less coding.

However, the Simple*Socket classes cannot be used to communicate with systems that do not use the same socket packet protocol. So, "LowerLevel" should be used as a starting point to communicate with other systems, but "Simple" should be used as a starting point when developing both the client and server ends.

Both examples demonstrate how to send objects over the network as messages. They are capable of sending a simple string message as well as a "complex" message which includes a GUID, DateTime, and a string. The message types are defined in a dll that is referenced by both client and server.

Both examples also include a function for retrieving all the IPv4 addresses of a computer (though this is not necessary for network communication).
Last edited Sep 23 2009 at 3:07 AM  by StephenCleary, version 5
Updating...
Page view tracker