Protocol Introduction


Before getting into the details of creating and using sockets, it s necessary to understand the protocols over which communication takes place. As mentioned earlier, the sockets interface is a generic interface that can be used to communicate over a variety of transport protocols. A transport protocol is a protocol that provides a standard mechanism for addressing each computer on the network. Chapter 7 discussed the IPv4 and IPv6 transport protocols and detailed how computers address one another using these protocols.

A transport protocol might encapsulate higher-level protocols that provide a certain set of characteristics to the application using that protocol. The TCP and UDP protocols can be encapsulated within either the IPv4 or IPv6 transport protocols. Figure 8-1 illustrates IPv4 protocol encapsulation. When an application sends data over a given transport protocol, the packet sent on the network will have a transport header, followed by any number of upper-layer protocols, and finally the data sent. In this example, we see an IPv4 header and a TCP header followed by the application s data.


Figure 8-1: Protocol encapsulation

TCP

TCP is a connection-oriented, reliable, stream-oriented protocol and is used for such things as Web traffic. When a TCP connection is established, a virtual circuit is formed . The computer initiating the connection sends a request to the destination. If the message is accepted, the destination computer will acknowledge it and respond with its own connection request back to the source. This way, each side can send traffic independently of one another. In effect, there are two separate data channels for sending and receiving data.

Traffic sent on a TCP connection is stream-oriented (not to be confused with the Stream class), which means that the protocol controls how and when data is sent and received on the connection. So, if the sender issues two send commands of 100 bytes each, the network stack on the receiver can return any number of bytes with a receive call. Applications that use streaming protocols should never make any assumptions on how many bytes are to be received in a single receive call, which is important if fixed- size messages are being sent. The receiving application must have logic to call receive enough times to retrieve an entire message. There s no maximum send size when sending data on a TCP connection, aside from resource limitations. For example, attempting to send a 100 megabyte (MB) buffer is likely to fail because of resource limits.

The advantage of TCP is that the communication is reliable. When data is sent, the underlying network stack ensures that the data is delivered reliably. If the data is lost or corrupted, the protocol will retransmit as necessary. If the data is lost and can t be recovered, an error will be returned to the application. The drawback to TCP is the overhead involved in setting up the virtual circuit and handling reliable data transmission, which is expensive, especially if the application needs to communicate with multiple end points or if the amount of data to send to each end point is fairly small.

UDP

UDP is a connectionless, unreliable, message-oriented protocol. UDP does not require any connection to be established. The sender simply calls the send method with a destination address where the packet should be sent. Delivery of the packet is not guaranteed by the protocol. If it s absolutely imperative that the recipient receives the packet, it s up to the application s logic to ensure that it s retransmitted until the receiver receives the data.

UDP is message oriented, which means that it preserves the message boundaries. For example, if the sender sends three 100-byte packets, the receiver will call receive three times, each returning 100 bytes. Preserving the message boundaries is extremely useful when the data being sent is of a fixed size and the receiver does not require any logic to assemble a single discrete message ”the message is preserved by the protocol. Note the maximum size of a UDP packet is 65,535 bytes; however, it s advisable to send small packets because large packets usually require fragmentation, which increases the probability that one fragment will be lost, causing the whole packet to be discarded at the receiver. Another benefit of UDP is that it s connectionless ”there s no overhead associated with establishing a connection. The sender simply indicates the destination of each packet.

Of course, the disadvantage of UDP is that it s unreliable. If the network is congested or other problems occur, it s possible that a UDP message will get lost, and both the sender and receiver will have no idea that it occurred unless the application builds loss detection as a part of the data being sent.




Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net