Wireless sensor network (WSN) is a well-developed domain suitable in the optimal collection and processing of information needed for the present world. For processing and transmitting the correct data by the network, it is affected by many disrupting factors like interference, battery life, distance between the nodes, and redundant data. There have been many methods proposed in the past. Among all the disrupting factors, routing has been the most studied problem in the WSN literature, whereas packet length sizing has been the most untouched topic compared to routing. Sizing the packet lengths to transmit it through the network is important to get error-free data and run an energyefficient network. In this paper, we propose a sling-shot spider optimization (S 2 SO) algorithm for packet length optimization in WSN and Internet of Things (IoT)-based networks. The S 2 SO algorithm is developed based on the spider's unique behavior of catching prey. The proposed algorithm is implemented in three types of networks: 1-hop, 2-hop, and multi-hop networks. A mathematical model for the communication channel, energy efficiency, and energy consumption is developed for all three types of networks.The proposed packet length optimization model is simulated in MATLAB software and compared with conventional bio-inspired algorithms. The results show that the proposed algorithm is very fast in finding the optimal results and transmits optimal packet size with low error rate of 0.2 p.u. and a high efficiency of 98%.