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: