| Description | The getsockname function retrieves the local address for a socket. 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 local 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 getsockname 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));
addrlen = sizeof (addr);
getsockname (sock, (SOCKADDR *)&addr, &addrlen);
printf ("Local 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 ("Local port: %d\n",ntohs (addr.sin_port));
while (1) {
res = recv (sock, dbuf, sizeof (dbuf), 0);
if (res <= 0) {
break;
}
procrec ((U8 *)dbuf);
}
closesocket (sock);
}
}
|