TCP, UDP, IP, ports and DNS
TCP, UDP, IP, ports and DNS
An IP address identifies a network endpoint at the IP layer; a transport port identifies a service endpoint within TCP or UDP. TCP provides an ordered byte stream, while UDP sends individual datagrams without guaranteeing delivery or order. DNS can resolve hostnames to addresses; an address may change and a name can have multiple addresses. Parsing an address is not a DNS lookup.
package main
import ( "fmt" "net")
func main() { for _, host := range []string{"192.0.2.10", "2001:db8::10", "example.test"} { fmt.Println(net.JoinHostPort(host, "8080")) }}JoinHostPort adds IPv6 brackets where necessary. These documentation addresses and reserved name are examples only; nothing is contacted. A TCP endpoint and a UDP endpoint may use the same numeric port because the transports have separate namespaces. An application protocol must define its own message semantics.
Expected output:
192.0.2.10:8080[2001:db8::10]:8080example.test:8080Inspect an endpoint without resolving it
package main
import ( "fmt" "net")
func main() { h, p, err := net.SplitHostPort("[2001:db8::5]:443") if err != nil { panic(err) } fmt.Println(h, p) fmt.Println(net.ParseIP(h) != nil) fmt.Println(net.ParseIP("example.test") == nil)}SplitHostPort separates syntax, not reachability; the port is still a string. ParseIP accepts IP literals, not hostnames. Resolver.LookupHost can perform name resolution using configured mechanisms, including DNS; live resolution is deliberately not used in this lab. Do not equate a successful lookup with an authorized or reachable service.
Expected output:
2001:db8::5 443truetrueTry it locally
Format IPv6 loopback ::1 with port 9000; split it again and print host and port.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Networking 01: TCP, UDP, IP, ports and DNS
Check the lesson objective before moving on.
What does TCP preserve?
What does JoinHostPort do?