#include /* for printf() */ #include /* for accept() */ #include /* for sockaddr_in and inet_ntoa() */ void DieWithError(char *errorMessage); /* Error handling function */ int AcceptTCPConnection(int servSock) { int clntSock; /* Socket descriptor for client */ struct sockaddr_in echoClntAddr; /* Client address */ unsigned int clntLen; /* Length of client address data structure */ /* Set the size of the in-out parameter */ clntLen = sizeof(echoClntAddr); /* Wait for a client to connect */ if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0) DieWithError("accept() failed"); /* clntSock is connected to a client! */ printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr)); return clntSock; }