The calls are working, but since you didn't bind the socket implicitly, the operating system or system library provided a port and default binding for you (exactly the same as when you call connect(2) without calling bind(2) first). Also, since you asked about the TCP stuff earlier, I'm assuming you're talking about internet sockets here.
Finding out what name the OS bound the socket to varies between operating systems, so you will have to look for your specific OS, but most operating systems provide a netstat or similar tool that you can use to query which applications are listening on which ports.,
As John mentions in a comment, you can use getsockname(2)
to later find a socket's name. Here is a short example:
// ...
// Create socket and set it to listen (we ignore error handling for brevity)
int sock = socket(AF_INET, SOCK_STREAM, 0);
listen(sock, 10);
// Sometime later we want to know what port and IP our socket is listening on
socklen_t addr_size = sizeof(struct sockaddr_in);
struck sockaddr_in addr;
getsockname(sock, (struct sockaddr *)&addr, &addr_size);
addr
will now contain the port and IP address that your socket is listening to.