是的,根据源代码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()(〜第2028行)刷新任何挂起的帧,并释放释放该地址的套接字.
您可以通过一个简单的示例来演示这一点 你需要netcat一个客户端和一个服务器.一个服务器,运行此代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
209 次 |
| 最近记录: |