Debugging-Use of setsockopt

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.