I am currently trying to send and receive UDP messages using a
TWSocket component. I have been successful receiving broadcast
messages and sending messages to a specific IP address, however when
attempting to send a broadcast, the SendTo() function returns with
-1. Below is the code I use when setting up the socket and sending of
messages:
Initial connection:
Socket->Proto = "udp";
Socket->Port = port;
Socket->Addr = "0.0.0.0";
Socket->Listen();
Sending:
memset(&socketDetails, 0, sizeof(sockaddr_in));
socketDetails.sin_addr.s_addr = dest; // dest is an unsigned long
(set to 0xFFFFFFFF when sending a broadcast)
socketDetails.sin_family = AF_INET;
socketDetails.sin_port = htons(port);
try {
if (Socket->State == wsConnected && Socket->SendTo
(socketDetails, sizeof(sockaddr_in), message, len) == len) {
return true;
}
}
catch (Exception &exception) {
// Exception handler
}
break;
If anyone could assist on whether this is the correct approach, or
even what it represents when SendTo() returns -1, then this would be
greatly appreciated.
Thanks.