title: Reuse the same socket to send and receive (Python)
body: <p>I have written a simple script to send and receive messages using the Python socket module. I want to first send a message using <code>sendMsg</code> and then receive a response using <code>listen</code>. <code>sendMsg</code> works fine but when my server sends a response I receive the error:</p>
  
  <p>"[WinError 10038] An operation was attempted on something that is not a socket"</p>
  
  <p>I close the socket connection in <code>sendMsg</code> and then try to bind it in <code>listen</code>, but it's at this line that the error is produced. Please could someone show me what I am doing wrong!</p>
  
  <pre><code>import socket
  
  port = 3400
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((socket.gethostname(), port))
  
  def sendMsg():
  
      print("\nSending message:\n\n")
      msg = ("Sample text").encode("utf-8")
      s.send(msg)
      s.close()
  
  def listen():
  
      s.bind(("", port))
      s.listen(1)
  
      serverSocket, info = s.accept()
      print("Connection from", info, "\n")
      while 1:
          try:            
              buf = bytearray(4000)
              view = memoryview(buf)
              bytes = serverSocket.recv_into(view, 4000)
              if bytes:
                  stx = view[0]
                  Size = view[1:3]
                  bSize = Size.tobytes()
                  nTuple = struct.unpack("&gt;H", bSize)
                  nSize = nTuple[0]
                  message = view[0:3+nSize]
                  messageString = message.tobytes().decode("utf-8").strip()
                  messageString = messageString.replace("\x00", "")
              else:
                  break
          except socket.timeout:
              print("Socket timeout.")
              break
  
  sendMsg()
  
  listen()
  </code></pre>
  
  <p><strong>Note:</strong> I have implemented <code>listen</code> in a separate client and used the line</p>
  
  <p><code>s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 3)</code></p>
  
  <p>before <code>s.bind()</code> and <code>s.connect()</code>. This works OK. It would be nice to have it all in one client though.</p>