Commit c893fa4a by “liusq”

新增gb28181日志11

parent 34258077
......@@ -128,8 +128,8 @@ unsigned short Common::calculate_checksum(void *b, int len) {
bool Common::pingAddress(const QString &address) {
QByteArray && byJsonIp =address.toUtf8();
const char * target= byJsonIp.data();
QByteArray &&byJsonIp = address.toUtf8();
const char *target = byJsonIp.data();
struct sockaddr_in dest;
struct hostent *host_entity;
......@@ -151,6 +151,16 @@ bool Common::pingAddress(const QString &address) {
return false; // 套接字创建失败,返回false
}
// 设置2秒的接收超时
struct timeval timeout;
timeout.tv_sec = 2; // 2秒
timeout.tv_usec = 0; // 0微秒
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
perror("setsockopt");
close(sock);
return false; // 设置超时失败,返回false
}
// 设置ICMP包头
struct icmphdr icmp_hdr;
icmp_hdr.type = ICMP_ECHO; // ICMP Echo Request
......@@ -175,9 +185,15 @@ bool Common::pingAddress(const QString &address) {
socklen_t addr_len = sizeof(recv_addr);
if (recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&recv_addr, &addr_len) <= 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// 超时错误处理
printf("Ping timeout.\n");
} else {
// 其他接收错误
perror("recvfrom");
}
close(sock);
return false; // 接收失败,返回false
return false; // 超时或接收失败,返回false
}
// 关闭套接字
......@@ -188,6 +204,7 @@ bool Common::pingAddress(const QString &address) {
//获取本机mask
bool GetLocalNetMask(const char *eth_inf,char* netmask_addr)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment