Wireless sensor networks (WSN) are rapidly growing in popularity, and their flexibility and ease of implementation cause an increasing number of security issues, making it critical to research network intrusion defense for wireless sensor networks. Denial of service (DoS) is a cyber-attack that shuts down the targeted network. A DoS attack on a WSN device will be fatal. It is prone to malicious attacks and difficult to prevent because every single node is independent of the others, yet there's no central or monitoring node, which is tough to avoid. Numerous lightweight authentication systems have been utilized in real-time to ensure encrypted communication. However, with the lack of synchronization between nodes during data routing, WSNs are highly prone to Denial of Service (DoS) attacks. This article combines the Adaptive Sunflower Optimization (ASFO) method with an improved Deep Convolutional Neural Network (IDCNN) to enhance the degree of security against DoS attacks on WSNs term ASFO-IDCNN. This paper initially utilizes the ASFO method to improve the initial values of IDCNN to prevent getting into the local optimum. Then, the ASFO-IDCNN technique is used to detect intrusions in WSNs. The results of numerous simulated situations are shown, and the associated data is compared. DoS protection research is precious in analyzing the anti-attack efficiency of WSN nodes. The influence of DoS attacks on the functioning of WSNs is considered in this research.