User Tools

Site Tools


tcp_udp

This is an old revision of the document!


TCP & UDP Communications

Author: Dylan Wallace Email: wallad3@unlv.nevada.edu
Date: Last modified on 04/30/17
Keywords: TCP/IP, UDP, Communications, Server, Client

tcp_vs_udp.jpg
The photo above depicts the differences between TCP & UDP connections which allow you to create different forms of communications. The big picture problem is high-latency or low-bandwidth telerobotics. Solving this partially or completely is important because it will allow remote operation of space robotics, disaster relief, etc. This tutorial shows you how to create a TCP client & server, and a UDP talker & listener, and takes approximately 2 hours to complete.

Motivation and Audience

This tutorial's motivation is to teach the basics of TCP & UDP communications. Readers of this tutorial assumes the reader has the following background and interests:

* Know how to program in C++
* Perhaps also know how to use the Linux command line
* Perhaps additional background needed may include TCP/IP protocols
* This tutorial may also attract readers who are interested in web programming


The rest of this tutorial is presented as follows:

Protocol Introductions

For a detailed overview of TCP & UDP communications, please see Beej's Guide to Network Programming. We will go over the concepts briefly in this section.

Internet vs Intranet

Networking is a term that describes connecting multiple computers together into a “network”, allowing them to communicate with each other. Networks can be local, or they can be global, like the Internet. A purely local network, not connected to the outside world, is called an intranet. This is typically a network that is very secure, and will only want to allow communications with computers that it trusts. The Internet (note the capital I) is the interconnection of all computers across the world. This network is not necessarily centralized, but is cascaded and “netted” between many different computers, allowing them all to communicate with each other by communicating through each other, or things like routers and switches. It is also possible to have individual internets (note the lowercase i), which are typically connected to the Internet as a whole, but also have local, more secure connections to each other. These are very common in modern businesses that utilize a network infrastructure. All of these types of networks utilize the TCP/IP protocols, which we will detail below.

IP Addresses

If you use the internet at all, you have probably heard of the term IP address. IP addresses are simply a number that describes a computer connected to the Internet. IP stands for Internet Protocol.

IP addresses have two forms:

  • IPv4: the older, more common protocol; has much less IP addresses available
  • IPv6: the modern, increasingly common protocol; has many many more addresses available

IPv4 was the first worldwide Internet protocol, and is still the most common protocol used today, due to its simplicity. IPv4 uses 4 unique bytes to describe an IP address, each with a range of 0-255, giving a total address of 32 bits. This amounts to around 4.3 billion IP addresses globally, which is not enough for the growing amount of computers in the modern world. This is what led to the development of IPv6 in the late 1990's.

IPv6 is becoming more and more popular, due to its enormous amount of IP addresses available. IPv6 uses 8 unique 16-bit sections, totalling a 128-bit address. This is nearly 3.4 x 10^38 unique addresses, more than enough for every atom on Earth to have its own IP address. This has essentially solved the IP address problem for the foreseeable future.

IPv4 example: 192.168.0.225
IPv6 example: 2001:db8:c9d2:12::51 (leading zeroes can be omitted)

Sockets

Sockets are the way that we create and utilize connections when programming communications. There are two types of sockets: datagram (UDP) and stream (TCP). Sockets are tied to individual ports, which are virtual sections of a computer that are dedicated to individual processes. This allows multiple applications to communicate with each other and other networks at the same time. Ports are 16-bit numbers, giving an average computer around 65,000 of them. However, the first 1,000 are reserved for important processes.

The picture above shows the concept of data encapsulation. In computer networks, there are many different protocols to deal with. In order to make the job easier on the program, we simplify the process by “encapsulating” our data into layers of protocols. We utilize headers to tell what kind and how much data is encapsulated within that layer. In general, computers follow the OSI network model for encapsulation:

1. Application
2. Presentation
3. Session
4. Transport
5. Network
6. Data Link
7. Physical

Where the application is what is directly using the data, such as TFTP, and the physical is the last line of sending/receiving from the computer, such as Ethernet. As a programmer, we do not need to worry about all of these protocols, thanks to encapsulation, because there are services on the computer's OS that deal with the lower-level headers. That means that we only have to deal with the headers that we care about. In an application programmers case, this may be TFTP or VOIP. In our case, this is the Transport/Network layer, or TCP/IP and UDP.

TCP

TCP stands for Transmission Control Protocol, and describes a connection that send a constant stream of bytes. TCP establishes a direct connection between the two computers, and send the bytes over this direct connection. The sending computer is called the server, and it waits for a computer to connect to a defined port. The receiving computer is called a client, and it establishes the connection with the server and receives the requested message. TCP is the most commonly used transmission protocol, because it ensures that data is received if sent. This is why TCP/IP is the protocol used for the Internet, especially for browsing the web. When you load a webpage, your computer establishes a TCP/IP connection with the server, and the server sends that webpage directly to your computer, ensuring a safe, complete delivery.

UDP

UDP stands for the User Datagram Protocol, and describes a connection that sends a packet of bytes. UDP sends a predefined amount of data all at once, and it is hopefully received at the other end. The sending computer is called the talker, and it sends the message to a specific IP address and port. The receiving computer is called the listener, and it “listens” to a given port for received messages. UDP does not guarantee delivery of a packet, but generally transmits the data much faster, due to no need to establish a direct connection. This is why UDP is typically used for large amounts of data.

TCP Client & Server

UDP Talker & Listener

Final Words

This tutorial's objective was to teach the basics of TCP & UDP communications. Complete source code for the 4 different programs was provided. Once the concepts were conveyed the reader could program and understand their own TCP & UDP communications programs.

Speculating future work derived from this tutorial, includes programming the TCP client & server and the UDP talker & listener to handle low bandwidth and high latency communications. In the big picture, the problem of high-latency or low-bandwidth telerobotics can be solved with this tutorial.

For questions, clarifications, etc, Email: wallad3@unlv.nevada.edu

tcp_udp.1493706466.txt.gz · Last modified: 2017/05/01 23:27 by dwallace