#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<ctype.h>
//#define SERVER "192.168.8.168" //IPC的IP地址
//#define PORT 8187 //端口号
//判断IP地址是否合法
int pa_chkip(const char *ip)
{
int i = 0;
int count[2] = {0};
const char *s = ".";
char TempIP[16];
memset(TempIP, 0, sizeof(TempIP));
int IPAddr[4] = {0};
int pos[3] = {0};
memcpy(TempIP, ip, sizeof(TempIP));
for(i = 0; i < strlen(TempIP); i++)
{
if(TempIP[0] != '.' && TempIP[i] == '.'
&& TempIP[i+1] != '\0' && TempIP[i+1] != '.')
{
count[0]++;
}
if(!isdigit(TempIP[i]))
{
count[1]++;
}
}
if(count[0] != 3 || count[1] != 3)
{
return -1;
}
IPAddr[0] = atoi(strtok(TempIP, s));
IPAddr[1] = atoi(strtok(NULL, s));
IPAddr[2] = atoi(strtok(NULL, s));
IPAddr[3] = atoi(strtok(NULL, s));
if ((IPAddr[0] >= 0&&IPAddr[0] <= 255)&&(IPAddr[1] >= 0&&IPAddr[1] <= 255)
&&(IPAddr[2] >= 0&&IPAddr[2] <= 255)&&(IPAddr[3] >= 0&&IPAddr[3] <= 255))
{
return 0;
}
else
{
return -1;
}
}
int pa_chkport(int port)
{
if (port < 0 && port > 65536)
{
return -1;
}else
{
return 0;
}
}