Description |
The getpeername function retrieves the address of the peer
to which a socket is connected.
The argument sock specifies a socket descriptor returned
from a previous call to socket.
The argument name is a pointer to the SOCKADDR structure.
If name is not NULL, the remote end IP address and port number
is filled in.
The argument namelen is a pointer to the address length. It
should initially contain the amount of space pointed to by
name. On return it contains the actual length of the address
returned in bytes.
The getpeername function is in the RL-TCPnet library. The
prototype is defined in rtl.h.
note
-
You must call the socket function before any other
function calls to the BSD socket.
-
If a negative number is returned, it represents an error
code.
|
Example |
#include <rtl.h>
__task void server (void) {
SOCKADDR_IN addr;
int sock, res, addrlen;
char dbuf[4];
while (1) {
sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
addr.sin_port = htons(1001);
addr.sin_family = PF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
bind (sock, (SOCKADDR *)&addr, sizeof (addr));
while (1) {
res = recv (sock, dbuf, sizeof (dbuf), 0);
if (res <= 0) {
break;
}
procrec ((U8 *)dbuf);
addrlen = sizeof (addr);
getpeername (sock, (SOCKADDR *)&addr, &addrlen);
printf ("Remote IP: %d.%d.%d.%d\n",addr.sin_addr.s_b1,
addr.sin_addr.s_b2,
addr.sin_addr.s_b3,
addr.sin_addr.s_b4);
printf ("Remote port: %d\n",ntohs (addr.sin_port));
}
closesocket (sock);
}
}
|