Target: Create a socket, use setsockopt to set its attribute to the broadcast type, and
This broadcast socket sends a broadcast to the System
Problem: The creation (socket), setting (setsockopt), and binding (BIND) are successful, that is, the sendto call fails.
Cause: The optval parameter of setsockopt () is not initialized and should be as follows:
Int optval = 1; (type conversion is required when calling ---Char *)
Or
Char optval = 1;
Setsockopt
TheSetsockoptFunction sets a socket option.
int setsockopt( SOCKET s, int level, int optname, const char* optval, int optlen);
Parameters
-
S
-
[In] descriptor identifying a socket.
-
Level
-
[In] level at which the option is defined; The supported
LevelS include
Sol_socketAnd ipproto_tcp. See Windows Sockets 2 protocol-Specific Annex for more information on protocol-specific levels.
-
Optname
-
[In] socket option for which the value is to be set.
-
Optval
-
[In] pointer to the buffer in which the value for the requested option is specified.
-
Optlen
-
[In] size of
OptvalBuffer, in bytes.
Return values
If no error occurs,SetsockoptReturns zero. Otherwise, a value of socket_error is returned, and a specific error code can be retrieved by callingWsagetlasterror.