এই নিবন্ধটি C++ কোড প্রোগ্রামিং এর মাধ্যমে সঠিক আইপি (ইন্টারনেট প্রোটোকল) ঠিকানা যাচাই করার উদ্দেশ্যে পরিবেশন করছে। আইপি অ্যাড্রেস হল একটি 32-বিট ডট-ডেসিমেল-নোটেশন, 0 থেকে 255 পর্যন্ত চারটি দশমিক সংখ্যার সেগমেন্টে বিভক্ত। উপরন্তু, এই সংখ্যাগুলি পরপর ডট দ্বারা পৃথক করা হয়। IP ঠিকানা তাদের মধ্যে একটি সংযোগ স্থাপন করার জন্য একটি অনন্য পদ্ধতিতে নেটওয়ার্কে একটি হোস্ট মেশিন সনাক্ত করার উদ্দেশ্যে কাজ করে৷
সুতরাং, ব্যবহারকারীর প্রান্ত থেকে সঠিক আইপি ঠিকানা ইনপুট যাচাই করার জন্য, নিম্নলিখিত অ্যালগরিদমটি সংক্ষিপ্ত করে যে কোডের ক্রমটি সঠিকভাবে সঠিক আইপি ঠিকানাটি সনাক্ত করার জন্য কীভাবে বাস্তবায়িত হয়েছে;
অ্যালগরিদম
START Step-1: Input the IP address Step-2: Spilt the IP into four segments and store in an array Step-3: Check whether it numeric or not using Step-4: Traverse the array list using foreach loop Step-5: Check its range (below 256) and data format Step-6: Call the validate method in the Main() END
অতঃপর, অ্যালগরিদম অনুসারে, আইপি ঠিকানা যাচাই করার জন্য নিম্নলিখিত c++ খসড়া তৈরি করা হয়েছে, যাতে সংখ্যাসূচক ফর্ম, পরিসর নির্ধারণ এবং ইনপুট ডেটা বিভক্ত করার জন্য কয়েকটি প্রয়োজনীয় ফাংশন নিযুক্ত করা হচ্ছে;
উদাহরণ
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// check if the given string is a numeric string or not
bool chkNumber(const string& str){
return !str.empty() &&
(str.find_first_not_of("[0123456789]") == std::string::npos);
}
// Function to split string str using given delimiter
vector<string> split(const string& str, char delim){
auto i = 0;
vector<string> list;
auto pos = str.find(delim);
while (pos != string::npos){
list.push_back(str.substr(i, pos - i));
i = ++pos;
pos = str.find(delim, pos);
}
list.push_back(str.substr(i, str.length()));
return list;
}
// Function to validate an IP address
bool validateIP(string ip){
// split the string into tokens
vector<string> slist = split(ip, '.');
// if token size is not equal to four
if (slist.size() != 4)
return false;
for (string str : slist){
// check that string is number, positive, and range
if (!chkNumber(str) || stoi(str) < 0 || stoi(str) > 255)
return false;
}
return true;
}
// Validate an IP address in C++
int main(){
cout<<"Enter the IP Address::";
string ip;
cin>>ip;
if (validateIP(ip))
cout <<endl<< "***It is a Valid IP Address***";
else
cout <<endl<< "***Invalid IP Address***";
return 0;
} একটি স্ট্যান্ডার্ড c++ এডিটর ব্যবহার করে উপরের কোডের সংকলনের পরে, নিম্নলিখিত আউটপুট তৈরি করা হচ্ছে যা সঠিকভাবে পরীক্ষা করছে যে ইনপুট নম্বর 10.10.10.2 একটি সঠিক আইপি ঠিকানা কি না নিম্নরূপ;
আউটপুট
Enter the IP Address:: 10.10.10.2 ***It is a Valid IP Assress***