Home | Overview | How Do I | Sample
This article describes stream sockets, one of the two Windows Socket types available. (The other type is the datagram socket.)
Stream sockets provide for a data flow without record boundaries — a stream of bytes that can be bi-directional (the application is full-duplex: it can both transmit and receive through the socket). Streams can be relied upon to deliver sequenced, unduplicated data. (“Sequenced” means that packets are delivered in the order sent. “Unduplicated” means that you get a particular packet only once.) Receipt of stream messages is guaranteed, and streams are well-suited to handling large amounts of data.
The network transport layer may break up or group data into packets of reasonable size. The CSocket class will handle the packing and unpacking for you.
Streams are based on explicit connections: socket A requests a connection to socket B; socket B accepts or rejects the connection request.
A telephone call provides a good analogy for a stream: under normal circumstances, the receiving party hears what you say in the order that you say it, without duplication or loss. Stream sockets are appropriate, for example, for implementations such as the File Transfer Protocol (FTP), which facilitates transferring ASCII or binary files of arbitrary size.
Stream sockets are preferable to datagram sockets when the data must be guaranteed to arrive and when data size is large. For more information about stream sockets, see the Windows Sockets specification. The specification is available in the Win32 SDK.
The MFC Advanced Concepts samples CHATTER and CHATSRVR use stream sockets. These samples might have been designed to use a datagram socket for broadcasting to all receiving sockets on the network. The present design is superior because (a) the broadcast model is subject to network flood (or “storm”) problems, (b) the client-server model adopted subsequently is more efficient, (c) the stream model supplies reliable data transfer, where the datagram model does not, and (d) the final model takes advantage of the ability to communicate between Unicode and ANSI socket applications that class CArchive lends to class CSocket.
Important If you use class CSocket, you must use a stream. An MFC assertion fails if you specify the socket type as SOCK_DGRAM.
See Also Windows Sockets: Background