Commit f38613f1 by “liusq”

新增gb28181日志08

parent eed904d5
...@@ -138,9 +138,9 @@ bool Common::sendPingRequest(int sockfd, const char *ipAddress, int sequence) { ...@@ -138,9 +138,9 @@ bool Common::sendPingRequest(int sockfd, const char *ipAddress, int sequence) {
icmpHeader.sequence = sequence; icmpHeader.sequence = sequence;
icmpHeader.checksum = 0; icmpHeader.checksum = 0;
char packet[sizeof(struct ICMPHeader) + 32]; char packet[sizeof(struct ICMPHeader) + 8];
memcpy(packet, &icmpHeader, sizeof(icmpHeader)); memcpy(packet, &icmpHeader, sizeof(icmpHeader));
memset(packet + sizeof(icmpHeader), 0, 32); memset(packet + sizeof(icmpHeader), 0, 8);
icmpHeader.checksum = calculateChecksum((uint16_t *)packet, sizeof(packet)); icmpHeader.checksum = calculateChecksum((uint16_t *)packet, sizeof(packet));
memcpy(packet, &icmpHeader, sizeof(icmpHeader)); memcpy(packet, &icmpHeader, sizeof(icmpHeader));
...@@ -157,6 +157,13 @@ bool Common::receivePingReply(int sockfd, int sequence) { ...@@ -157,6 +157,13 @@ bool Common::receivePingReply(int sockfd, int sequence) {
char buffer[1024]; char buffer[1024];
struct sockaddr_in srcAddr; struct sockaddr_in srcAddr;
socklen_t srcAddrLen = sizeof(srcAddr); socklen_t srcAddrLen = sizeof(srcAddr);
struct timeval tv;
tv.tv_sec = 2; // 设置超时时间为2秒
tv.tv_usec = 0;
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
perror("setsockopt");
return false;
}
ssize_t bytesRead = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&srcAddr, &srcAddrLen); ssize_t bytesRead = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&srcAddr, &srcAddrLen);
if (bytesRead < 0) { if (bytesRead < 0) {
...@@ -187,7 +194,8 @@ bool Common::pingAddress(const QString &address) { ...@@ -187,7 +194,8 @@ bool Common::pingAddress(const QString &address) {
perror("socket"); perror("socket");
return 1; return 1;
} }
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
int sequence = 1; int sequence = 1;
if (sendPingRequest(sockfd, ipAddress, sequence)) { if (sendPingRequest(sockfd, ipAddress, sequence)) {
if (receivePingReply(sockfd, sequence)) { if (receivePingReply(sockfd, sequence)) {
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include <linux/route.h> #include <linux/route.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <netdb.h> #include <netdb.h>
#include <fcntl.h>
#define NAME_SIZE 8 #define NAME_SIZE 8
#define MAC_SIZE 18 #define MAC_SIZE 18
......
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