Addresses, Ports and Sockets in C++¶
Before sending data, identify the destination and the operating-system resource used to reach it. This program prints socket addresses and creates a UDP socket without sending a message.
Course examples on GitHub
Before you start¶
Complete the development setup. Run from the same examples/network directory on the Pi.
Outcome: explain the difference between an address, a port and a socket, then identify who closes the descriptor.
Picture an endpoint¶
| Name | Meaning in this example |
|---|---|
| IP address | 127.0.0.1 reaches this computer through IPv4 loopback. |
| Port | 9000 identifies the UDP service endpoint on that address. |
| Socket | A kernel communication resource, accessed through an integer descriptor. |
TCP port 9000 and UDP port 9000 are separate. Neither a port number nor a descriptor is an IP address.
Build and run¶
The first command prints:
The IPv6 command prints IPv6 address=::1 port=9000 before the same socket message. It inspects an IPv6 address; the demonstration socket itself is deliberately IPv4.
Follow the code¶
Complete file: app/addresses/main.cpp.
getaddrinfo() returns a list of possible socket addresses. AF_UNSPEC allows IPv4 and IPv6, while SOCK_DGRAM asks for datagram endpoints. Numeric loopback inputs work without external DNS; a hostname such as localhost may return several entries according to local configuration.
The returned list needs freeaddrinfo(). The explicit cleanup at the bottom of main() releases it. getnameinfo() formats each endpoint numerically; it does not send a request to that port. These resolver APIs report their own error codes, formatted with gai_strerror(), rather than the socket API's errno.
Release the resources explicitly¶
socket() returns an integer file descriptor or -1 on failure. The example keeps that integer in fd and calls close(fd) explicitly at the bottom of main(). The same cleanup section runs after either success or the error handler.
The address list has a separate lifetime: freeaddrinfo(addresses) releases it. Both resources start in an empty state so the cleanup section can check whether acquisition succeeded. There is no custom socket class or course header to follow.
Try a different input¶
The number and order of results can vary. Resolving a name does not prove that a service is listening. A resolver error is different from a failed bind or a receive timeout.
Check your understanding¶
The program prints port 9000. Has it reserved port 9000?
Compare your reasoning
No. It only resolves and prints an endpoint. Reserving a local UDP endpoint requires binding a socket, which the next lesson demonstrates.
Continue the course¶
Previous: Development Setup · Course overview · Next: Your First UDP Exchange
Reference: getaddrinfo and its result list.