Yes, according to the source code https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/udp.c?id=refs/tags/v3.19-rc6, udp_destroy_sock()
(~line 2028) flushes any pending frames, and releases the socket which frees the address.
You can demonstrate this with a simple example. You'll need netcat
, a client and a server. One the server, run this code:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
int main() {
struct sockaddr_in me;
int sock;
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
perror("socket error:");
return -1;
}
memset(&me, 0, sizeof(me));
me.sin_family = AF_INET;
me.sin_port = htons(60000);
me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr*)&me, sizeof(me)) == -1) {
perror("bind error: ");
return -1;
}
printf("On client execute:\n");
printf(" nc -u {servers ip address} 60000\n\n");
printf("type: hello world<enter>\n");
printf("Hit enter when you've done this...");
getchar();
printf("\nNow check the input queue on this server\n");
printf(" netstat -an|grep 60000\n");
printf("Notice that we have buffered data we have not read\n");
printf("(probably about 360 bytes)\n");
printf("Hit enter to continue...");
getchar();
printf("\nI'm going to end. After I do, run netstat -an again\n");
printf("and you'll notice port 60000 is gone.\n\n");
printf("Re-run this program on server again and see you\n");
printf("have no problem re-acquiring the UDP port.\n");
return 0;
}