Question: I want to make sure I'm understanding how this works. Specifically the term "Sending Port ID" has me very confused.

Consider this diagram, I want to make sure I have root port selection figured out:

[![enter image description here](https://i.stack.imgur.com/lIZZI.jpg)](https://i.stack.imgur.com/lIZZI.jpg)

The switch on top is the root and all settings are at the defaults.

Both of SW2's ports connected to SW3 are designed because it has the lowest path cost to the root (its the only path to the root).

Here is what I'm unsure of: SW3's 0/4 is the Root Port because the Sending Port ID of 0/1 wins over 0/2

Is this correct? The Port IDs of SW3 have no bearing in the selection it is only the Sending Port ID's of SW2 that matter right?

Answer: 
you need to understand the next 

Spanning-Tree Port Roles  

 - Root Port (RP) - It is a port on a non-root switch, which is the shortest (the best) path towards the root bridge. (i.e. port 0/4 0/3 in SW3)  

 - Designated Port (DP) - It is a port that is in the forwarding state. (i.e. port 0/1 0/2 SW2)  

 - Non-Designated Port (NDP) - It is a port that is in a blocking state in the STP topology.

so your question is about which port in SW3 will be selected as ***Root Port***

As soon as the root has been elected, all non-root switches begin to calculate which port is the best ***(the least cost)*** towards the root bridge. This port will be called the root port. 

***What if the Root Cost Path is identical?***

1. Prefer the lowest Root Path Cost.
2. In case of the same Root Path Cost, prefer the lowest Bridge ID of the designated switch (the neighbor that sends BPDUs).
3. In case of receiving BPDUs on multiple ports from the same designated switch (BPDU sender), ***prefer the lowest Port ID (known also as port priority) of the sender***. That parameter has a default value 128 and is configurable.
4. In case of all above are did not resolve the problem, prefer the lowest Port ID of the BPDU sender.

so answer of your question (SW3's 0/4 is the Root Port because the Sending Port ID of 0/1 wins over 0/2)is ***YES***

please make use of this very usful linke [STP](http://ciscoiseasy.blogspot.com.eg/2010/10/lesson-20-spanning-tree-protocol.html)
Some corrections to the above answer. Port-id and port-priority are different. One is configurable. The other is internally set. I'll make an attempt to answer your question below.

There has to be just 1 root port per switch. By definition , this is the port with the lowest **path-cost** to root. 

* A path-cost is the sum of all port-costs along a given path. And
port-cost is set by the speed of the link connected to a given
port.

Now lets look at your diagram. SW3 has 2 paths to the root, and both go through SW2. Assuming that both the links out of SW3 are the same speed, the port-cost is identical. So the path cost is the same out of both its ports. Now we need a tie-breaker, cuz we can have only 1 root port.

By default, STP uses the bridge-id of neighboring switch to break the tie. But since the neighboring switch is the same switch here (SW2), STP next uses **port-priority of the neighboring switch** to break the tie. This is something that can be manually configured. But lets say that in your case, its at default, and therefore at the same setting on both switches. So STP falls to something that **has** to be unique. And this final fallback is **the internal port-id**. This is not the same as the interface number (0/1,0/2..) but is an integer that is mapped to the interface number internally. And usually lower interface numbers, are mapped to lower ordered port-ids.