title: Error sending socket [WinError 10061]No connection could be made because the target machine actively refused it for Socket from Python to Android
body: <p>I am sending a socket from a python script to my android phone and vice versa. When my android sends a packet to my python script it works but sending packets from python script to android gives this error:</p>
  
  <pre><code>Error sending socket  [WinError 10061] 
  No connection could be made because the target machine actively refused it
  </code></pre>
  
  <p>Note: When sending from android to python the function uses a different socket and different port. The error occurs here <code>s.connect((host, port))</code>
  Here is my python code to send the packet:</p>
  
  <pre><code>try:
      s = socket.socket()
      host = "ip_address_of_android"
      port = 7801
      s.connect((host, port))
      print("connected")
      s.listen(5)
      print("sending")
      text = "hello"
      s.sendall(text.encode())
      s.close()
  except Exception as e:
      print("Error sending socket ", e)
  </code></pre>
  
  <p>And here is my android studio code to receive the packet:</p>
  
  <pre><code>public String receives() {
      Socket socket;
      DataInputStream ds;
      try {
          socket = new Socket("ip_address_of_android", 7801);
          ds = new DataInputStream(socket.getInputStream());
          boolean done = false;
          while (!done) {
              result = ds.readUTF();
          }
      } catch (Exception e) {
          System.out.print("error");
      }
      return result;
  }
  </code></pre>
  
  <p>I am assuming it is an android firewall error but I have no idea how to fix this. Thanks in advance.</p>