Determine where client is from
TAYBENGH%NUSDISCS.BITNET at cunyvm.cuny.edu
TAYBENGH%NUSDISCS.BITNET at cunyvm.cuny.edu
Sat Mar 24 16:31:04 AEST 1990
Dear Marvin,
In Unix wizard u write:
>I have a client and server program. I would like the server program to log
>where the client is calling from (hostname). So if I were to call the server
>, the server would print: from: oliver.vcu.edu
>I'm pretty sure this isn't a wizard-type question, but we don't subscribe
>to usenet news here, there is no other applicable listserv group for me to
>ask/join, and there aren't any wizs around here.
If u are using BSD socket interface, the u can use recvfrom() to get
the caller's identity. Below are the portion of code doing it.
I hope this helps...
-Beng Hang Tay (email address: taybengh at nusdiscs.bitnet)
Department of Information Systems and Computer Science
National University of Singapore
-------------------------cut here------------------------------------
int size, socklen, socket_id;
struct sockaddr_in from_sock; /* caller's sockaddr_in */
struct hostent *from;
/* recvfrom() will return caller's identity in from_sock */
socklen = sizeof(from_sock);
size = recvfrom(socket_id, buf, buflen, option,
(struct sockaddr *)&from_sock, &socklen);
if (size < 0)
errmsg("bs_recvfr(): sendto()")
/* display caller identity */
printf("\nfrom sock: \n");
printf("family = %d \n", from_sock.sin_family);
printf("sin_addr.s_addr = %x \n", ntohl(from_sock.sin_addr.s_addr));
printf("net number = %x \n", inet_netof(from_sock.sin_addr));
printf("local address part = %x \n", inet_lnaof(from_sock.sin_addr));
printf("port # = %u\n", ntohs(from_sock.sin_port));
/* get the caller's host name */
from = gethostbyaddr((char*)&from_sock.sin_addr,
sizeof(from_sock.sin_addr), type);
/* display caller host name */
printf("\nfrom host: \n");
printf("name %s \n", from->h_name);
printf("address type %d \n", from->h_addrtype);
printf("address length %d \n", from->h_length);
printf("Internet address %s, %x \n", from->h_addr,
ntohl(from->h_addr));
---------------------------------cut here---------------------------
More information about the Comp.unix.wizards
mailing list