ICMP pinger 프로그램에서 작업 중이며 문제가 발생했습니다. 내가 먼저 터미널에 sudo su
수행 프로그램을 실행하면 우리가 원시 소켓 그리고 내가 프로그램을 실행할 때 내가이 역 추적 여기[Errno 56] : 소켓이 이미 연결된 수정 프로그램입니까?
sh-3.2# python3 icmp.py
Pinging 31.13.66.112 using Python:
Traceback (most recent call last):
File "icmp.py", line 149, in <module>
ping("www.facebook.com")
File "icmp.py", line 141, in ping
delay = doOnePing(dest,timeout)
File "icmp.py", line 123, in doOnePing
sendOnePing(mySocket,destAddr,myID)
File "icmp.py", line 111, in sendOnePing
mySocket.sendto(packet,(destAddr,1))
OSError: [Errno 56] Socket is already connected
을 얻을 내가 코드는 사용하는 데 필요한 것 때문에 그래서 내가 루트 액세스 권한이
import os
from socket import *
from sys import *
from struct import *
from time import *
from select import *
from binascii import *
ICMP_ECHO_REQUEST = 8
def checksum (val):
csum = 0
countTo = (len(val) // 2) * 2
count = 0
while count < countTo:
thisVal = val[count+1] * 256 + val[count]
csum = csum + thisVal
csum = csum & 0xffffffff
count = count + 2
if countTo < len(val):
csum = csum + val[len(val) - 1]
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
def receiveOnePing(mySocket,ID,timeout,destAddr):
timeLeft = timeout
while 1:
startedSelect = time()
whatReady = select([mySocket],[],[],timeLeft)
howLongInSelect = (time() - startedSelect)
if whatReady[0] == []: #Timeout
return "Request timed out."
timeReceived = time()
recPacket, addr = mySocket.recvfrom(1024)
icmpHeader = recPacket[20:28]
kind,code,checksum,idNum,sequence = unpack("bbHHh",icmpHeader)
if idNum == ID:
sizeofdouble = calcsize("d")
timeSent = unpack("d",recPacket[28:28+sizeofdouble])[0]
print(("TYPE: %d CODE: %d CHECKSUM: 0x%08x ID: %d SEQ: %d TIME: %d ms\n" % (kind,code,checksum,idNum,sequence,timeReceived - timeSent)*1000))
timeLeft = timeLeft - howLongInSelect
if timeLeft <= 0:
return "Request timed out."
else:
return "Reply from %s successfully." % destAddr
def sendOnePing(mySocket, destAddr, ID):
myChecksum = 0
header = pack("bbHHh", ICMP_ECHO_REQUEST, 0, myChecksum, ID, 1)
data = pack("d",time())
myChecksum = checksum(header + data)
if platform == 'darwin':
myChecksum = htons(myChecksum) & 0xffff
else:
myChecksum = htons(myChecksum)
header = pack("bbHHh", ICMP_ECHO_REQUEST,0,myChecksum,ID,1)
packet = header + data
mySocket.sendto(packet,(destAddr,1))
def doOnePing(destAddr,timeout):
icmp = getprotobyname("icmp")
mySocket = socket(AF_INET,SOCK_RAW,icmp)
mySocket.connect((destAddr,80))
myID = os.getpid() & 0xFFFF
sendOnePing(mySocket,destAddr,myID)
delay = receiveOnePing(mySocket,myID,timeout,destAddr)
mySocket.close()
return delay
def ping(host,timeout = 1):
dest = gethostbyname(host)
print("Pinging " + dest + " using Python:")
print()
while 1:
delay = doOnePing(dest,timeout)
print(delay)
sleep(1)
return delay
ping("www.facebook.com")
실행 우리는 "골격"프로그램 실험실 시트를 받았고 우리는 누락 된 부분에 너무 Here is the lab sheet where the skeleton code was given
메모를 입력했다 : 골격과 내 프로그램 실 거예요 사실 때문에 그에게 완벽하게 일치 나는 물건을 바꿔야 만했다. o 이전에 발생한 다른 오류를 수정하십시오. 나는 사전에 당신의 도움을 주셔서 감사
,
타일러 그래서
소켓을 만들 때'SO_REUSEADDR' 옵션 사용을 고려하십시오. – Hyperboreus
어떻게 구현하나요? 나는 소켓 프로그래밍에 대해 매우 새로운 것이다. 고맙습니다! – heyItsTy1992