Port number of socket returned by accept()

Hi,

I typed a few tcp/ip client/server examples from a book and it works - sort of - but I noticed something strange. When I run my server I set it to use port 3001 and the client uses the same port to connect to server. They succeed, but the server prints something that doesn't really make much sense: it prints that the client uses port 30152. Next time I run it is 30153 and next time is 30154 and so on.

Here is the code of the server, accept() call and printf() call.

      struct sockaddr_in  clntAddr, testAddr;
      socklen_t           clntAddrLen = sizeof (struct sockaddr_in);

      int                 clntSock = accept (servSock, (struct sockaddr *)&clntAddr, &clntAddrLen);

      if (clntSock < 0)
         DieWithSystemMessage ("accept() failed");

      char  clntName[INET_ADDRSTRLEN];  // str to contain client address
      
      if (inet_ntop(AF_INET, &clntAddr.sin_addr.s_addr, clntName, INET_ADDRSTRLEN))
         printf ("Handling client %s/%hu\n", clntName, ntohs(clntAddr.sin_port));

I am on Mac (10.5) and the book is TCP/IP Sockets in C. (Full source code link - TCPEchoClient4.c & TCPEchoServer4.c + DieWithMessage.c & Practical.h)