How to find client's IP address (or name)

"Kapoor, Nishikant" <nishikant.x.kapoor-CDGYnrjxVPGpp/[email protected]> Tue, 1 Mar 2005 09:17:02 -0600
Newsgroups gmane.user-groups.linux.delhi.devel,gmane.user-groups.linux.delhi
Message-ID <B4448B75FF74124ABB5793D8BFBEEA3901C13C47@exccpgo04.corp.xcelenergy.com>
[Sorry for cross posting].

I have a very basic server written in C. It creates a socket, binds it to a port and listens for client's requests. I am trying to find the IP address of the client when the client attempts to make a connection. Does anyone know how to do that? (I was hoping to have client structure filled in when client establishes connection, but that doesn't seem to be the case.)

Here is the trimmed code (can send complete code if you so desire).

Tanks,
Nishi

#includes

int main() {
   struct sockaddr_in  server;
   struct sockaddr     client;
   struct hostnet      *hp;

   sock1 = socket(AF_INET, SOCK_STREAM, 0);

   memset(&server, 0, sizeof(server));
   server.sin_family      = "127.0.0.1";
   server.sin_addr.s_addr = htonl(INADDR_ANY);
   server.sin_port        = htons(atoi(argv[1]));
   rc = bind(sock1, (struct sockaddr *)&server, sizeof(server));

   rc = listen(sock1, 32);

   rc = listen(sock2, 32);

  while (1) {
      printf("Waiting on select()...\n");
      rc = select(32, &sock, NULL, NULL, &timeout);

      rc = accept(msgSock, &client, &addrlen);

      fprintf (stdout, "client.sa_data=%s \n", client.sa_data);

      fprintf (stdout, "Client's IP address: %s \n", "???");
      fprintf (stdout, "Client's port no: %s \n", "???");
   }

} /* end of main() */