Wireless sensor network is a self-configured, infrastructure less wireless network consisting of a large number of sensor nodes equipped with specialized sensors that can monitor various physical attributes such as temperature, pressure, vibration and sound. WSN relies on hardware simplicity to make sensor field deployments both affordable and long lasting without any maintenance support. Security and energy efficiency is the most important concerns in wireless sensor networks (WSNs) design. Sensor nodes are powered up with batteries. Due to unattended nature of deployment, the sensor nodes cannot be recharged again. In this condition, the nodes must optimally consume power. Energy constrained sensor networks periodically place nodes to sleep in order to extend the network lifetime. Various protocols are designed to reduce the energy consumption of sensor nodes by keeping the antenna in sleep mode 90% of time, so that power is saved. MAC protocols are designed to vary the sleep time based on the communication need. However, attackers use their knowledge of their underlying MAC protocol, to reduce the sleep time of the node, so that life time of the node reduces. This problem we refer it as Denial of sleep attack and in this paper we propose effective solution to defend against this attack on a sensor network.