48

My router has two protocols (and a "both" option) that I can select when setting up port forwarding: UDP and TCP. What is the difference between these two protocols and when would you select one over the other in port forwarding?

Murali Suriar
  • 10,166
  • 8
  • 40
  • 62
Guy
  • 1,798
  • 4
  • 21
  • 28

11 Answers11

76

You can find a good summary here:

What is the difference between UDP and TCP internet protocols?

Both TCP and UDP work at transport layer TCP/IP model, but have very different usage.

The most important differences are:

  • Reliability:
    TCP: connection-oriented
    UDP: connectionless
  • Ordered:
    TCP: order of message receipt is guaranteed
    UDP: order is not guaranteed
  • Protocol weight:
    TCP: heavyweight, because of the connection/ordering overhead
    UDP: lightweight, very few overhead
  • Packets:
    TCP: streaming, data is read as a "stream," with nothing distinguishing where one packet ends and another begins. There may be multiple packets per read call.
    UDP: datagrams, one packet per one read call.

Frame structure

When data is sent over the network, it needs to be encapsulated into so called "frames." There are various methods of encapsulation depending on the protocol and topology that are being used. The following images show how TCP and UDP frame structures differ.

This is the TCP frame structure:

TCP frame

An this the UDP frame structure, much simpler:

UDP frame

Typical protocols which use TCP are HTTP, FTP and SMTP. Examples of protocols using UDP are DNS and DHCP.

splattne
  • 28,348
  • 19
  • 97
  • 147
  • @splattne - order of message _receipt_ is guarantee, not transmission – Alnitak May 17 '09 at 06:46
  • @Alnitak: of course, you're right. I meant the receipt is guaranteed in the transmission order. I'll update to make that clear. – splattne May 17 '09 at 07:19
53

TCP is backed by acks and retries to make sure you data gets where it's going. UDP is connectionless and "fire and forget". UDP is mostly used for streaming type applications, where if you lose some data you don't need to try to send it again.

Which one you use depends on the application. For example, a web server uses TCP.

  • 3
    That makes sense - so if you're streaming a video there's not point in resending a missing frame later because you've already passed that point. Thanks – Guy May 16 '09 at 20:38
  • 15
    +1. Just to add, anyone looking for more in depth understanding should read one of W. Richard Stevens many excellent books on the subject. "TCP/IP Illustrated, v1" and "UNIX Network Programming" serve as excellent tutorials and references. – Murali Suriar May 16 '09 at 23:47
47

And the CEO level explanation:

UDP is when you throw your paper in the general direction of the bin.

TCP is when it misses, you throw exact copies of the same paper again and again until it falls into the bin. There would be paper wastage, even resent TCP packets result in wastage of network or system resources.

Mark Henderson
  • 68,316
  • 31
  • 175
  • 255
pgs
  • 3,471
  • 18
  • 19
8

TCP and UDP are both protocols that run on top of IP. TCP has guaranteed delivery and UDP does not. You would select one or the other for port forwarding depending on what service you're trying to forward. HTTP, for instance is TCP. If you don't know what protocol the service you're trying to forward is, it's almost certainly TCP.

Brian
  • 925
  • 2
  • 12
  • 20
4

To answer other part of your question, you have to forward what your application uses. To forward HTTP traffic, select TCP. To forward TFTP traffic, select UDP. p2p programs mostly use both tcp and udp, so forward them both.

It all depends on the protocol and the program you are using.

hayalci
  • 3,611
  • 3
  • 25
  • 37
  • you are not entirely right. dns uses also tcp/53, not so often but still [ eg. for zone transfer ]. – pQd May 24 '09 at 10:40
  • yes. Also, it is entirely valid to send regular dns queries over tcp. I changed the example to TFTP, which exclusively uses UDP. – hayalci May 25 '09 at 11:30
2

TCP makes an effort to ensure the data made it to the recipient; UDP does not.

J. Polfer
  • 449
  • 2
  • 5
  • 9
  • Wrong. The IP protocol is not reliable and data loss can occur. TCP makes an effort to ensure that sent data (which is split into small parts and sent via IP packets) will make it to the recipient (by eventually resending small parts if required). – Anonymous Jul 13 '09 at 18:57
  • 1
    @ Anonymous - You basically restated what I said above. How am I wrong? – J. Polfer Sep 22 '09 at 14:07
  • I think you'll both find that IP is the underlying framework (stack) and the basic communication protocol that both UDP and TCP use. You can't actually communicate useful data over the internet using JUST IP, as far as I know -- even basic pings etc. use a protocol on top of IP (the other main one; ICMP). – Lee B Apr 25 '10 at 23:34
2

As well as differences mentioned so far TCP also provides

flow control. Using a window mechanism the recipient limits how many octets (bytes) it wants to receive from the sender before the sender must wait for permission to send more data. The size of the window can be changed by the receiver as the conversation proceeds. This allows a receiver to 'throttle' the rate of data coming from a sender: UDP does not allow this and all a receiver can do is throw away data it cannot buffer or process on arrival.

some more technical facilities such as non-contiguous acknowledgement (uses selective acknowledgements to allow the sender to retransmit just a missing frame rather than the missing frame and subsequent ones that the receiver already has but could not acknowledge in the usual way), congestion handling and adaptive retransmission (adjusting the timer that decides how soon to resend a transmitted frame when an acknowledgement is not received). These are not relevant to UDP but help TCP to provide extra services over UDP without greatly increasing the network load for the same payload volume.

mas
  • 639
  • 5
  • 9
1

Never mind all the answers explaining one vs. the the other. For the purposes of setting up a port-forward, the rules are pretty simple: you need to choose TCP, unless the docs for the protocol/application you're using say to use UDP. Usually the ones that use UDP are related to streaming, or very simple things that favour lightweight protocols/speed over reliability (particularly DNS and NTP). Note that there are other protocols besides TCP and UDP too -- ICMP, tunneling protocols, etc.

Lee B
  • 3,380
  • 1
  • 17
  • 15
1

The more wellknowned a protocol is, the more routers on the path intervenes with the protocol. Send tcp packets, and there's a big probability the received packets are highly mangled. UDP is usually not that mangled, and the other 254 IP paylod protocols are usually completely untouched.

0

Well both are network protocols built on top of something called Internet Protocol or IP. The Internet Protocol is the major protocol of the Internet and is for example what allows us to browse the web. That's why you will often see these protocols written as TCP/IP or UDP/IP. They are actually core protocols of the internet.

In both cases your data is split up into chunks called packets. TCP ensures that these packets get to their destination and in the correct order and without any errors.

UDP on other hand will not ensure that a data packet will even arrive. It will only ensure that if it does arrive it will be without error.

Typically, other protocols will run on top of TCP and UDP and are known as application protocols because they operate within an application and not within the operating system. Your web browse uses HTTP protocol which will use the TCP protocol.

Domain name lookups or DNS queries often use UDP but can also use TCP. A number of streaming services use UDP beneath another application protocols.

I hope that answers your question in simpler english.

hookenz
  • 14,132
  • 22
  • 86
  • 142
0

User Datagram Protocol (UDP)

It is part of the base protocols of the Internet Protocol Suite. Programs on networked computers can send short messages sometimes called as datagrams. UDP does not guarantee any reliability( it happens datagram may arrive out of order, are duplicated, or are missing without any notice). The fact that no checking whether all packets are actually delivered is made, UDP proves to be faster and more efficient, for applications that do not need guaranteed delivery. UDP find its uses in such situations:

Time-sensitive applications. The problems due to delayed packets are avoided

It is also useful for servers that answer small queries from huge numbers of clients. UDP supports packet broadcast (conveys to all on local network) and multicasting (conveys to all subscribers).

Transmission Control Protocol (TCP)

It is often referred to as TCP/IP due to the importance of this protocol in the Internet Protocol Suite. TCP operates at a higher level, concerned only with the two end systems, (e.g. between web browser and a web server). TCP provides reliable, sequential delivery of a stream of data from one program on one computer to another program on another computer. Common uses of TCP regroup e-mailing support and file transfer and Web applications. Among its management tasks, TCP controls message size, the rate at which messages are exchanged, and network traffic congestion. As for IP, it handles lower-level transmissions from computer to computer as a message transferred across the Internet.