Monday, 13 August 2012

Art of C# Sockets II: "Lineage" of NetworkStream

What is a NetworkStream? Specifically what makes it unique from its abstract base class Stream.

A network stream is like a stream of water, only instead of H20 going from the top of the mountain to the valley below, we instead have "molecules" of data (which we call bytes)  travelling continuously from a source to a destination. If the source and desination are at different parts of a computer network, the connecting byte stream can be loosely termed a "NetworkStream".

Ok, so we know vaguely know what a Network Stream, as they call it, is all about. Where does the NetworkStream class come from? What's its lineage, so to speak, in terms of the object model/class hierarchy.

Stream is the abstract base class of all Streams (the top of the Himalayas, if you will). Stream is the only class that NetworkStream directly subclasses from. Indirectly, NetworkStream also inherits from MarshalByRefObject and System.Object but these are rather peripheral facts (we'll look at the esoteric implications much later).

NetworkStream is a specialization of a Stream that supports reading, writing and seeking for Streams over the Network and lives in the System.Net.Sockets namespace. The class is very useful when writing TCP clients and servers.

NetworkStream extends System.IO.Stream. Has methods to read and write byte arrays. Streams also provide seeking capabilities - ability to query and modify the current position within a stream.

What is the practical usage of NetworkStream?

To send messages from TCP client to server.

C# Sockets Programming (Intro to System.Net.Sockets)

Desperate Programmer: I want to write network software and not be bound by weirdo libraries that limit me in how many connections I can make and such like.
Wise Programmer: Then what you need is Sockets. Master sockets and cross the impasse of data transfer across networks.
Adage: Don't trust a Dot Net Programmer to architect an application who has never programmed network software in C# and moreover has never programmed Sockets.
Desperate Programmer: So, then how do I unlock the Sockets functionality of .Net?
Wise Programmer: Start with the System.Net.Sockets namespace and progress from there

Concept Check: What is the Relation between TCP and Sockets?

Socket is a SOFTWARE OBJECT -> that REPRESENTS an endpoint for inter-process communication. Sockets can use various PROTOCOLS (rulesets), such as TCP and UDP.

How do you create a TCP Client in C#? Tell me PRECISELY.

The answer is SIMPLE. You can guess it without knowing C#. Use the TcpClient class. Where does that great TCP client class live? Ans: System.Net.Sockets (part of System.dll).

Suppose your TCP client is aptly named client. To connect the client to a socket, you need to do client.Connect(IPEndPoint). The endpoint consists of an IP address and a port. Now we have a connection. The server has to be up though for this connection to work.

How do you send a message from the Client to the Server?

You need to grab the NetworkStream from the TcpClient.  You can do stuff with the Stream, like write byte arrays to it, and Flush it. To convert a string to a byte array, you can use the class ASCIIEncoding (System.Text, from mscorlib.dll) which represents an encoder. Calling GetBytes on the encoder creates a byte buffer.

How do you create a TCP Server in C#? Again, a precise explanation is requested.
This is just a TcpListener. You create a TcpListener using IPAddress.Any, and the required port.  Calling Start() on the listener starts it listening to incoming requests. The method AcceptTcpClient returns the next TCP client accepted by the listener. Once you have the client object, you can spawn a new thread to handle the client communication.

Writing a TCPClient is child's play. Writing a TCP server is a little more involved.