You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
func checkSum(data []byte) uint16 {
var (
sum uint32
length = len(data)
index int
)
for length > 1 {
sum += uint32(data[index])<<8 + uint32(data[index+1])
index += 2
length -= 2
}
if length > 0 {
sum += uint32(data[index])
}
sum += sum >> 16
return uint16(^sum)
}
将checkSum替换成使用这种方法实现,程序是可以正常运行的,和书中描述的预期一致
The text was updated successfully, but these errors were encountered:
func checkSum(msg []byte) uint16 {
sum := 0
//假设为偶数
for n := 1; n < len(msg)-1; n += 2 {
sum += int(msg[n])*256 + int(msg[n+1])
}
sum = (sum >> 16) + (sum & 0xffff)
sum += (sum >> 16)
var answer uint16 = uint16(^sum)
fmt.Println(answer)
return answer
}
62207
阻塞
func checkSum(data []byte) uint16 {
var (
sum uint32
length = len(data)
index int
)
for length > 1 {
sum += uint32(data[index])<<8 + uint32(data[index+1])
index += 2
length -= 2
}
if length > 0 {
sum += uint32(data[index])
}
sum += sum >> 16
var answer uint16 = uint16(^sum)
fmt.Println(answer)
return answer
}
https://github.com/qiniu/gobook/blob/master/chapter5/icmptest.go
)n, err := conn.Read(msg[0:])
,运行结果和书中描述的预期不一致The text was updated successfully, but these errors were encountered: