Commit 6aca3f45 by 郭峰

Merge branch 'feature-1007488' into 'release'

新增gb28181日志11

See merge request !56
parents 516fd6bb c893fa4a
...@@ -128,8 +128,8 @@ unsigned short Common::calculate_checksum(void *b, int len) { ...@@ -128,8 +128,8 @@ unsigned short Common::calculate_checksum(void *b, int len) {
bool Common::pingAddress(const QString &address) { bool Common::pingAddress(const QString &address) {
QByteArray && byJsonIp =address.toUtf8(); QByteArray &&byJsonIp = address.toUtf8();
const char * target= byJsonIp.data(); const char *target = byJsonIp.data();
struct sockaddr_in dest; struct sockaddr_in dest;
struct hostent *host_entity; struct hostent *host_entity;
...@@ -151,6 +151,16 @@ bool Common::pingAddress(const QString &address) { ...@@ -151,6 +151,16 @@ bool Common::pingAddress(const QString &address) {
return false; // 套接字创建失败,返回false 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包头 // 设置ICMP包头
struct icmphdr icmp_hdr; struct icmphdr icmp_hdr;
icmp_hdr.type = ICMP_ECHO; // ICMP Echo Request icmp_hdr.type = ICMP_ECHO; // ICMP Echo Request
...@@ -175,9 +185,15 @@ bool Common::pingAddress(const QString &address) { ...@@ -175,9 +185,15 @@ bool Common::pingAddress(const QString &address) {
socklen_t addr_len = sizeof(recv_addr); socklen_t addr_len = sizeof(recv_addr);
if (recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&recv_addr, &addr_len) <= 0) { if (recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&recv_addr, &addr_len) <= 0) {
perror("recvfrom"); if (errno == EAGAIN || errno == EWOULDBLOCK) {
// 超时错误处理
printf("Ping timeout.\n");
} else {
// 其他接收错误
perror("recvfrom");
}
close(sock); close(sock);
return false; // 接收失败,返回false return false; // 超时或接收失败,返回false
} }
// 关闭套接字 // 关闭套接字
...@@ -188,6 +204,7 @@ bool Common::pingAddress(const QString &address) { ...@@ -188,6 +204,7 @@ bool Common::pingAddress(const QString &address) {
//获取本机mask //获取本机mask
bool GetLocalNetMask(const char *eth_inf,char* netmask_addr) 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