title: [Errno 56]: Socket already connected fix?
body: <p>I am working on a ICMP pinger program and have run into a problem. When i run the program i first do <code>sudo su</code> in the terminal so i have root access since we're required to use raw sockets and then when i run the program i get this traceback</p>
  
  <pre><code>sh-3.2# python3 icmp.py
  Pinging 31.13.66.112 using Python:
  
  Traceback (most recent call last):
    File "icmp.py", line 149, in &lt;module&gt;
      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
  </code></pre>
  
  <p>here is the code I am running</p>
  
  <pre><code>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 &lt; countTo:
  
          thisVal = val[count+1] * 256 + val[count]
  
          csum = csum + thisVal
  
          csum = csum &amp; 0xffffffff
  
          count = count + 2
  
      if countTo &lt; len(val):
  
          csum = csum + val[len(val) - 1]
  
          csum = csum &amp; 0xffffffff
  
      csum = (csum &gt;&gt; 16) + (csum &amp; 0xffff)
  
      csum = csum + (csum &gt;&gt; 16)
  
      answer = ~csum
  
      answer = answer &amp; 0xffff
  
      answer = answer &gt;&gt; 8 | (answer &lt;&lt; 8 &amp; 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 &lt;= 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) &amp; 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() &amp; 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")
  </code></pre>
  
  <p>we were given a lab sheet with the "skeleton" program and we had to fill in the missing parts so <a href="http://media.pearsoncmg.com/aw/ecs_kurose_compnetwork_6/programmingAssignments/Socket5_ICMPpinger.pdf" rel="nofollow">Here is the lab sheet where the skeleton code was given</a></p>
  
  <p>note: the skeleton and my program wont match perfectly due to the fact that i had to change things to fix other errors that occurred before this one.</p>
  
  <p>I thank you for your help in advance,</p>
  
  <p>Tyler</p>