[Network programming/C ++] solution for error 10013 in Network Programming

Source: Internet
Author: User

After having struggled with the problem of sending DHCP packets for more than one day, after modifying the design mode of the entire program, the original packets were sent separately, changed to a class that specifically sends data packets and another class that specifically processes data packets, and then the program is particularly easy to modify ~~

To put it bluntly, the main problem today is to use socket for broadcast, but the error code is 10013. I learned through the query that this problem is caused by insufficient interesting permissions. However, I have never heard of any permission issues in the socket until now, namely, permission |.

After I asked my colleague, I realized that it was a function called setsockopt. After modification, the empty DHCP packet will be successfully sent ~~~ O (partition _ partition) O

 

Implementation Code:

 

// Send data packets <br/> bool dhcpconnectcontrol: sendtoserver (dhcp_header package) <br/>{< br/> clientsocket = socket (af_inet, sock_dgram, ipproto_udp ); // create a UDP socket <br/> bool bbroadcast = true; <br/> setsockopt (clientsocket, sol_socket, so_broadcast, (const char *) & bbroadcast, sizeof (bool )); // set broadcast permissions </P> <p> If (clientsocket = invalid_socket) // socket creation failed <br/>{< br/> cout <"invalid socket" <Endl; <br/> wsacleanup (); <br/> Return false; <br/>}< br/> // configure protocol cluster <br/> clientsock_in.sin_family = af_inet; <br/> clientsock_in.sin_addr.s_un.s_addr = htonl (inaddr_broadcast ); <br/> clientsock_in.sin_port = htons (dhcpsendport); <br/> If (connect (clientsocket, (sockaddr *) & clientsock_in, sizeof (sockaddr) = socket_error) // establish a connection <br/>{< br/> cout <"failed to connect. "<Endl; <br/> closesocket (clientsocket); <br/> wsacleanup (); <br/> retur N false; <br/>}< br/> cout <"IP:" <inet_ntoa (clientsock_in.sin_addr) <Endl; <br/> cout <"Port: "<ntohs (clientsock_in.sin_port) <Endl; <br/> int err = sendto (clientsocket, (char *) & package, sizeof (package), 0, (sockaddr *) & clientsock_in, sizeof (sockaddr_in); // data packet transmission <br/> If (ERR = socket_error) // data transmission error <br/>{< br/> DWORD res = wsagetlasterror (); <br/> cout <"Transmission Error: "<res <Endl; <br/> closesocket (clientsoc Ket); <br/> wsacleanup (); <br/> return false; <br/>}< br/> cout <"data packet sent successfully! "<Endl; <br/> closesocket (clientsocket); <br/> return true; <br/>}< br/>

 

 

Running result:

 

 

 

 

 

 

Use Wireshark to capture packets as follows:

 

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.