假设创建一个UDP Socket:
socketHandle = socket(serverAddr->ai_family, serverAddr->ai_socktype, serverAddr->ai_protocol);
如今就用它来发送Broadcast。一定会出错的,Socket API不同意这样做。那么怎样使它支持broadcast呢?须要继续做一些设置:
int broadcastPermission = 1; setsockopt(socketHandle, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission, sizeof(broadcastPermission));
完整的UDP broadcast的样例见:《Socket tips: UDP Echo service - Client code》
时间: 2024-11-10 13:02:01