MFC中使用UDP发送广播

/**********************配置远程广播套接字,将数据发送给客户机*****************************************/
broadcast_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (broadcast_socket == SOCKET_ERROR){
MessageBox(L"创建broadcast_socket套接字失败",L"联网失败", MB_OK);
return;
}
local.sin_family = AF_INET;
local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
local.sin_port = htons(BROADCAST_PORT);


if (bind(broadcast_socket, (sockaddr*)&local, sizeof(local)) == SOCKET_ERROR){
MessageBox(L"绑定本地BROADCAST_PORT端口失败", L"联网失败", MB_OK);
return;
}
bool bBroadcast = true;
setsockopt(broadcast_socket, SOL_SOCKET, SO_BROADCAST, (const char*)&bBroadcast, sizeof(bool));

你可能感兴趣的:(MFC中使用UDP发送广播)