364 12. Network Programming
TCP. Transmission control protocol (TCP) provides the reliable delivery of data. Large
messages are automatically broken into smaller IP-sized packets, transmitted, received,
and reassembled in order by the receiver. Packets that are lost during transmission are
automatically retransmitted. e reliability of TCP message delivery can result in long delays
(on the order of seconds) if packets are lost and must be retransmitted. For this reason,
TCP is typically not considered suitable for the delivery of real-time data. TCP establishes
a connection between the two devices; this connection is maintained until broken by one
or both devices. In our implementation, only one TCP connection is permitted at a time.
If the other device breaks the connection, an error will result when attempting to send or
receive data.
UDP. User datagram protocol (UDP) is a connectionless protocol. A connectionless protocol
supports communicating with multiple devices from a single interface. UDP provides no
guarantee of data delivery. Data packets may arrive out of order, may be duplicated, or may
not be delivered at all. UDP is typically used for time sensitive applications where the timely
delivery of data is more important than reliability.
12.1.1 Addresses
Each device on the network must have a unique IP address. e original addressing scheme,
known as IPv4, uses a 32-bit number for each address. is allows for 2
32
(4,294,967,296)
addresses. Because of the large number of devices now connected to the Internet and the
way these addresses are allocated, we are quickly running out of unused addresses. A new
addressing scheme, known as IPv6, has been introduced. IPv6 uses a 128-bit number for
each address. is allows for 2
128
(approximately 3.4 × 10
38
) addresses—to put it in perspec-
tive, with IPv6 there are enough addresses to give every grain of sand on the earth its own IP
address and still have a bunch le over. e IPv4 addressing scheme is still the most widely
used, so we will be using it in this book.
12.1.2 Port
Each addressable device also has a range of port numbers that are used. A port number is
a 16-bit number. is yields 2
16
(65,536) port numbers. We might equate the IP address
with the address of a building and the port number with a room in the building. Port
numbers 0–1023 are predened for use in well-known services and should be avoided.
Port numbers 1024–65535 may be freely used.
12.1.3 Socket
In network programming, a socket is an interface to the network. For this reason, network
programming is oen referred to as socket programming. For Windows programming, Win-
sock is the API that supports the creation of sockets that support TCP/IP and UDP/IP.
Sockets may be congured as blocking or nonblocking. A blocking socket does not return
from function calls to send or receive data until the operation is complete. A nonblocking
socket always returns immediately, even if no data was sent or received. Given the real-time
nature of many games, we cannot aord to wait for socket operations to complete. If block-
ing sockets were used, we would need to write a multithreaded application with a separate