C socket 绑定 DNS端口错误



 // Create a socket
server_s = socket(AF_INET, SOCK_DGRAM/*SOCK_STREAM*/, IPPROTO_UDP/*0*/);
if(server_s == -1){
    fprintf(stderr, "socket error.\n");
    exit(-1);
}
// Fill-in my socket's address information and bind the socket
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(53);
server_addr.sin_addr.s_addr = inet_addr("192.168.137.1");/*htonl(INADDR_BROADCAST);*//*htonl(INADDR_ANY);*/
if(bind(server_s, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1){
    fprintf(stderr, "Bind error.\n");
    exit(-1);
}

在win7下设置防火墙允许访问53端口,绑定192.168.137.1:80可以顺利实现监听,绑定53后bind便会返回-1错误。为什么绑定会失败? 下面是DNS端口抓包: 请输入图片描述

c 网站 C++ socket

锈涩的联装炮 10 years, 7 months ago

有可能你的电脑此端口已经被DNS Client占用。。 命令行模式下 netstat -an 查看 UDP端口53是否已经存在。。

菊花啊消逝吧 answered 10 years, 7 months ago

Your Answer