Due to the progress of unmanned aerial vehicles (UAVs), this new technology is widely applied in military and civilian areas. Multi-UAV networks are often known as flying ad hoc networks (FANETs). Due to these applications, FANET must ensure communication stability and have high scalability. These goals are achieved by presenting clustering techniques in FANETs. However, the characteristics of these networks, like high-mobility nodes, limited energy, and dynamic topology, have created great challenges in two important processes of clustering protocols, namely cluster construction and the selection of cluster heads. In this paper, an intelligent clustering scheme based on the whale optimization algorithm called ICW is suggested in flying ad hoc networks. Firstly, each UAV specifies its hello interval based on the lifespan of adjacent links to guarantee the adaptability of ICW to FANET. Then, a centralized clustering process is done using a whale optimization algorithm (WOA) to find the best cluster centers on the network. To determine the membership of each UAV in a cluster, ICW employs a new criterion, i.e. closeness ratio, so that each UAV joins a cluster with the best closeness ratio. In addition, the evaluation of each whale is carried out based on a fitness function, consisting of three components, namely the number of isolated clusters, the ratio of inter-cluster distance to intra-cluster distance, and cluster size. Then, a cluster head is selected for each cluster based on a score value. This score is dependent on the weighted sum of four metrics, namely remaining energy, the average link lifespan between each UAV and its neighbors, neighbor degree, and the average distance between each UAV and its neighbors. In the last step, two routing processes, namely intra-cluster routing and inter-cluster routing, are introduced in FANET. Then, the evaluation and implementation of ICW is performed through the NS2 simulator. After completing the simulation process, ICW is compared to MWCRSF, DCM, and GWO, and the evaluation results are presented in two scenarios, namely network evaluation in the clustering process and network evaluation in the routing process. Accordingly, in the first scenario, ICW has low clustering time and a high cluster lifetime. In the second scenario, ICW optimizes energy consumption, network longevity, packet delivery rate, routing overhead, and delay compared to other approaches. However, throughput in ICW is about 3.9% lower than that in MWCRSF.