计算机网络相关题目及答案(第五章实验)

本文档详细介绍了计算机网络实验中关于套接字编程的作业——使用Dijkstra算法计算从不同节点(t, u, v, w, y, z)到全网最短路径的过程,提供了源代码和每一步的计算解答。" 135330293,11400593,数据结构与网络基础知识点解析,"['数据结构', '计算机网络', '操作系统', '计算机组成原理']
摘要由CSDN通过智能技术生成

实验:套接字编程作业5:ICMP ping

源代码如下:

# -*- coding: utf-8 -*-
"""
SAFA_LIYT
"""
import socket
import os
import sys
import struct
import time
import select
import binascii

ICMP_ECHO_REQUEST = 8

# 计算checksum
def checksum(str):
    csum = 0
    countTo = (len(str) / 2) * 2
    count = 0
    while count < countTo:
        thisVal = str[count+1] * 256 + str[count]
        csum = csum + thisVal
        csum = csum & 0xffffffff
        count = count + 2
        
    if countTo < len(str):
        csum = csum + str[len(str) - 1].decode()
        csum = csum & 0xffffffff

    csum = (csum >> 16) + (csum & 0xffff)
    csum = csum + (csum >> 16)
    answer = ~csum
    answer = answer & 0xffff
    answer = answer >> 8 | (answer << 8 & 0xff00)
    return answer

# 客户机接收服务器的Pong响应
def receiveOnePing(mySocket, ID, sequence, destAddr, timeout):
    timeLeft = timeout

    while 1:
        startedSelect = time.time()
        whatReady = select.select([mySocket], [], [], timeLeft)
        howLongInSelect = (time.time() - startedSelect)
        if whatReady[0] == []: # Timeout
            return "Request timed out."

        timeReceived = time.time()
        recPacket, addr = mySocket.recvfrom(1024)

        #Fetch the ICMP header from the IP packet
        #获得ICMP_ECHO_REPLY结构体,取出校验和checksum、序列号ID、生存时间TTL
        #获得ICMP_ECHO_REPLY结构体,取出校验和checksum、序列号ID、生存时间TTL
        
        header = recPacket[20: 28]
        type,code,checksum,packetID,sequence = struct.unpack("!bbHHh", header)
        if type == 0 and packetID == ID:
            byte_in_double = struct.calcsize("!d")
            timeSent = struct.unpack("!d", recPacket[28: 28 + byte_in_double])[0]
            delay = timeReceived - timeSent
            ttl = ord(struct.unpack("!c", recPacket[8:9])[0].decode())
            return (delay, ttl, byte_in_double)
        
        timeLeft = timeLeft - howLongInSelect
        if timeLeft <= 0:
            return "Request timed out."

# 客户机向服务器发送一个Ping报文
def sendOnePing(mySocket, ID, sequence, destAddr):
    # Header is type (8), code (8), checksum (16), id (16), sequence (16)
    
    myChecksum = 0
    # Make a dummy header with a 0 checksum.
    # struct -- Interpret strings as packed binary data
    header = struct.pack("!bbHHh", ICMP_ECHO_REQUEST, 0, myChecksum, ID, sequence)
    data = struct.pack("!d", time.time())
    # Calculate the checksum on the data and the dummy header.
    myChecksum = checksum(header + data)

    # Get the right checksum, and put in the header
    #if sys.platform == 'darwin':
    #    myChecksum = socket.htons(myChecksum) & 0xffff
    #    Convert 16-bit integers from host to network byte order.
    #else:
    #    myChecksum = socket.htons(myChecksum)
    
    header = struct.pack("!bbHHh", ICMP_ECHO_REQUEST, 0, myChecksum, ID, sequence)
    packet = header + data
    
    # AF_INET address must be tuple, not str
    #Both LISTS and TUPLES consist of a number of objects
    #which can be referenced by their position number within the object
    mySocket.sendto(packet, (destAddr, 1)) 
# 客户机发出一次Ping请求
def doOnePing(destAddr, ID, sequence, timeout):
    icmp = socket.getprotobyname("icmp")

    #SOCK_RAW is a powerful socket type. For more details see: http://sock-raw.org/papers/sock_raw
    #SOCK_RAW:原始套接字
    #普通的套接字无法处理**ICMP**、IGMP等网络报文,而**SOCK_RAW**可以;

    #Create Socket here
    #创建一个套接字,使得客户机和服务器相关联
    mySocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
    
    # 向服务器发送一个Ping报文
    sendOnePing(mySocket, ID, sequence, destAddr)
    
    # 从服务器接收一个Pong报文
    delay = receiveOnePing(mySocket, ID, sequence, destAddr, timeout)

    mySocket.close()
    return delay

# 要调用的主体程序
def ping(host, timeout=1):
    # timeout=1 means: If one second goes by without a reply from the server,
    # the client assumes that either the client’s ping or the server’s pong is lost
    dest = socket.gethostbyname(host)
    print("Pinging " + dest + " using Python:")
    print("")
    # Send ping requests to a server separated by approximately one second
    
    myID = os.getpid() & 0xFFFF
    loss = 0
    for i in range(10): # 向服务器发出4次Ping请求
        result = doOnePing(dest, myID, i, timeout)
        if not result:
            print("第"+str(i)+"次Ping请求超时(>1s)")# 响应超时,丢包数量+1
            loss += 1
        else:
            delay = int(result[0]*1000)
            ttl = result[1]
            bytes = result[2]
            print("第"+str(i)+"次Ping请求成功:")
            print("收到的Pong响应消息:"+dest+":byte(s)="+str(bytes)+"delay="+str(delay)+"ms TTL="+str(ttl))
        time.sleep(1)
    print("发送次数:"+str(10)+" 发送成功次数:"+str(10-loss)+" 丢包次数:"+str(loss))
    return 
ping("www.baidu.com")

最短路径

步骤

N

D(t)p(t)

D(u)p(u)

D(v)p(v)

D(w)p(w)

D(y)p(y)

D(z)p(z)

0

X

--

--

3,x

6,x

6,x

--

1

Xv

7,v

6,v

3,x

6,x

4,v

--

2

Xvy

7,v

6,v

3,x

6,x

4,v

18,y

3

Xvyu

7,v

6,v

3,x

6,x

4,v

18,y

4

Xvyut

7,v

6,v

3,x

6,x

4,v

12,t

5

  • 23
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小嘤嘤怪学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值