最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

C++ Winsock UDP Port Wont Bind - Stack Overflow

programmeradmin5浏览0评论

I'm writing a bit of code that can listen for UDP messages via Winsock. The port bind works on local loopback (127.0.0.1) but when I try to change to any other IP the bind fails.

Is there something I'm missing?

int socket_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

sockaddr_in receive_port;
memset(&receive_port, 0, sizeof(receive_port));
std::string ip_addr = "192.168.1.11";
receive_port.sin_addr.s_addr = inet_addr(ip_addr.c_str());
receive_port.sin_family = AF_INET;
receive_port.sin_port = htons(4000);

const char optval = 1;
int result = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int));
int bind_result = bind(socket_, (const struct sockaddr*)&receive_port, sizeof(receive_port));

I'm writing a bit of code that can listen for UDP messages via Winsock. The port bind works on local loopback (127.0.0.1) but when I try to change to any other IP the bind fails.

Is there something I'm missing?

int socket_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

sockaddr_in receive_port;
memset(&receive_port, 0, sizeof(receive_port));
std::string ip_addr = "192.168.1.11";
receive_port.sin_addr.s_addr = inet_addr(ip_addr.c_str());
receive_port.sin_family = AF_INET;
receive_port.sin_port = htons(4000);

const char optval = 1;
int result = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int));
int bind_result = bind(socket_, (const struct sockaddr*)&receive_port, sizeof(receive_port));
Share Improve this question asked Mar 20 at 11:54 MaxAMaxA 135 bronze badges 9
  • 1 Is 192.168.1.11 the address of your computer? Does the port need permissions from the default Windows firewall? What is the error code you get? – Richard Critten Commented Mar 20 at 12:01
  • 2 "the bind fails" isn't a very useful problem description. What is the error? Is 192.168.1.11 a valid local address? – G.M. Commented Mar 20 at 12:01
  • Please provide an error message for clarification and what are you trying to achieve. – Nikita Struk Commented Mar 20 at 12:05
  • What happens if you replace ...s_addr = inet_addr(ip_addr.c_str()); with ...s_addr = INADDR_ANY;? – G.M. Commented Mar 20 at 12:24
  • Thanks for the replies! I get a 10049 error so does this mean the IP isn't valid? In practice this will be ran on one PC on a local network receiving UDP messages from another PC on the same network. The IP and port should be valid where it's being ran but the bind still fails. However, it still works on 127.0.0.1. – MaxA Commented Mar 20 at 12:29
 |  Show 4 more comments

1 Answer 1

Reset to default 2

You can only bind a port to interface addresses that belong to the local PC that is running your program.

However, INADDR_ANY (0.0.0.0) is almost always the right thing to use, unless for some complex reason you want to only receive packets on one of your interfaces, and you have more than one. If you are just on an ordinary system plugged into one ethernet cable/wifi, using ANY is easiest.

发布评论

评论列表(0)

  1. 暂无评论