tinyrpc/test/ipaddrtest/main.cc

34 lines
825 B
C++
Raw Normal View History

2024-12-25 19:40:02 +08:00
#include <iostream>
#include <netinet/in.h>
using namespace std;
bool check(string ip){
int num_stat = 0; // 0-3
int dot_stat = 0; // 0-3
for(char ch : ip) {
if(std::isdigit(ch)) {
if(dot_stat < 0 || dot_stat > 3) {
return false;
}
num_stat += 1;
if(num_stat > 3) return false;
}else if(ch == '.'){
if(num_stat < 1 || num_stat > 3) {
return false;
}
dot_stat += 1;
num_stat = 0;
if(dot_stat > 3) return false;
} else {
return false;
}
}
return true;
}
int main() {
sockaddr_in addr{};
cout << addr.sin_addr.s_addr;
}