title: Server- Server communication Python socket- OSError: [Errno 99] Cannot assign requested address
body: <p>I have tried to create a simple socket program where I can enable back and forth communication between two server sockets. The first iteration runs successfully and then there is one set of message passing that is possible. When it comes to second round of message passing I get the error.
  I feel there is some mistake in the IP Address but I could not resolve it.</p>
  
  <p>I have looked here but could not find a solution </p>
  
  <p><a href="https://stackoverflow.com/questions/47286678/oserror-errno-99-cannot-assign-requested-address-py">OSError: [Errno 99] Cannot assign requested address - py</a></p>
  
  <p><a href="https://stackoverflow.com/questions/48306528/python-socket-error-cannot-assign-requested-address">Python - socket.error: Cannot assign requested address</a></p>
  
  <p>Any help is deeply appreciated </p>
  
  <h3>This is Server 1:</h3>
  
  <pre><code>import socket 
  import requests
  
  host = "127.0.0.1"
  #ip address
  port_other_send = 5007
  #Other's port while sending
  port_own_send = 5006
  #Our port for sending
  port_other_recieve = 5009
  #other port for recieving 
  port_own_recieve = 5008
  #our port for recieving
  #s = socket.socket()
  #s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  #s.bind(("", port_own)) 
  
  
  #binds the socket element to the IP address and the Port    
  
  def main():
  
      send()
  
  def send(): 
      s = socket.socket()
      s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      s.bind((host, port_own_send))
      s.connect((host,port_other_recieve))
      message = input("Type message to be sent ")
      while message != "q":   
          s.send(message.encode('utf-8'))
          break   
      receive()
  def receive():
      print("This works yo")
      socketva = socket.socket()
      socketva.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      socketva.bind((host, port_own_recieve)) 
  
      socketva.listen()
      c,addr=socketva.accept()
      x = True
      while x ==True:
          print("Connection from",(addr))
          data = c.recv(1024)
          print ("Data recieved ", str(data))
          response= ("Data recieved")
          c.send(data)
          x = False
      c.close
      send()
  
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <h3>This is server 2:</h3>
  
  <pre><code>import socket 
  import flask
  
  host = "127.0.0.1"
  #ip address
  port_other_send= 5006
  #Other's port while sending
  port_own_send= 5007
  #Our port for sending
  port_other_recieve = 5008
  #other port for recieving 
  port_own_recieve=5009
  #our port for recieving 
  
  #binds the socket element to the IP address and the Port
  
  def main():
  
      receive()
  
  def receive():
      socketva = socket.socket()
      socketva.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      socketva.bind((host, port_own_recieve)) 
  
      socketva.listen()
      c,addr=socketva.accept()
      x = True
      while x== True: 
          print("Connection from",(addr))
          data = c.recv(1024)
          print ("Data recieved: ",data)
          response= ("Data recieved")
          c.send(data)
          x= False
      c.close
      send()
  
  def send():
      s = socket.socket()
      s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      s.bind((host, port_own_send))
      s.connect(("",port_other_recieve))  
      message = input("Enter reply message ")
      while message != "q":   
          s.send(message.encode('utf-8'))         
          receive()
  
  if __name__ == "__main__":
          main()
  </code></pre>
  
  <h3>Server 1 Output:</h3>
  
  <pre><code>Type message to be sent hi
  This works yo
  Connection from ('127.0.0.1', 5007)
  Data recieved  b'hihihihihihihihihihihihihihihihihi'
  Traceback (most recent call last):
    File "Server_socket.py", line 100, in &lt;module&gt;
      main()
    File "Server_socket.py", line 57, in main
      send()
    File "Server_socket.py", line 68, in send
      receive()
    File "Server_socket.py", line 87, in receive
      send()
    File "Server_socket.py", line 63, in send
      s.connect((host,port_other_recieve))
  OSError: [Errno 99] Cannot assign requested address
  </code></pre>
  
  <h3>Server 2 Output:</h3>
  
  <pre><code>Connection from ('127.0.0.1', 5006)
  Data recieved:  b'hi'
  Enter reply message hi
  Traceback (most recent call last):
    File "Server_socket2.py", line 54, in &lt;module&gt;
      main()
    File "Server_socket2.py", line 23, in main
      receive()
    File "Server_socket2.py", line 41, in receive
      send()
    File "Server_socket2.py", line 50, in send
      s.send(message.encode('utf-8'))         
  ConnectionResetError: [Errno 104] Connection reset by peer
  </code></pre>