In this paper, a new intrusion detection system (IDS) is presented to deal with distributed denial of service (DDoS) attacks. A combined algorithm based on Harris Hawks Optimization (HHO) and Dragonfly Algorithm (DA) is proposed to select relevant features and eliminate irrelevant and redundant features from the NSL-KDD dataset. The extracted features are presented to a multilayer perceptron (MLP) neural network. This network (as a classifier) divides the network traffic into two classes, normal and attack categories. Performance of the proposed model is evaluated with two standard and widely-used datasets in the field of intrusion detection: NSL-KDD and UNSW-NB15. The results of the simulations clearly show the superiority of the proposed method compared to the previous methods in terms of critical evaluation criteria such as accuracy, precision, recall, and F-Measure. Specifically, the proposed method exhibited improvements of 96.9%, 97.6%, 96%, and 96.8% in these metrics, respectively (compared to the baseline method). The main reason for these improvements is the ability of the combined algorithm to intelligently select the optimal features and reduce the dimensions of the data. This careful selection of features allows the MLP neural network to focus on critical information, increasing the classification accuracy and ultimately improving the performance of the intrusion detection system. This research showed that combining optimization algorithms and machine learning works well. So, it is effective for tackling DDoS attacks. It can lead to better intrusion detection systems. These systems will be more efficient and accurate.