Server/Client application TimeoutError on LAN
<p>I have two programs: server.py and client.py. I need to be able to use server.py in my main PC, and client.py from my laptop. When I run them, I get the following error from client.py:</p>

<pre><code>TimeoutError: [WinError 10060]
</code></pre>

<p>I have disabled firewalls in both my PC (that runs Windows 7) and my laptop (that runs Windows 8).</p>

<p>How do I get them to connect?</p>

<p>Some things that I have tried:</p>

<ul>
<li>Creating Firewall port rules, on the PC.</li>
<li>Disabling the firewall in both computers.</li>
<li>Using different ports.</li>
<li>Changing the server address from "localhost" to socket.gethostname(), this changes the error from <code>TimeoutError</code> to <code>ConnectionRefusedError</code>.</li>
</ul>

<p>The IP for my PC is 192.168.0.2, and I am sure of this because I have an Apache server running in port 80, and that one works (I can access that from my laptop).</p>

<p>Python versions: PC: 3.5.2, Laptop: 3.4.1</p>

<h2>Code</h2>

<p>server.py:</p>

<pre><code>import socket
import threading

server_port = 2569
server_address = "localhost"

class ClientThread(threading.Thread):
    def __init__(self, client_info):
        super(ClientThread, self).__init__()
        self.client_info = client_info

    def run(self):
        socket = self.client_info[0]
        bytes_received = socket.recv(100)
        print(bytes_received.decode("utf-8"))

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((server_address, server_port))
server_socket.listen(5)

while True:
    new_client = server_socket.accept()
    ClientThread(new_client).run()
</code></pre>

<p>client.py:</p>

<pre><code>import socket

server_port = 2569
server_address = "192.168.0.2"

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.connect((server_address, server_port))
server_socket.send(b"message")
</code></pre>

<p>You just need to change the <code>localhost</code> or <code>socket.gethostname()</code> in the <code>server.py/client.py</code> scripts to the actual <strong>internal ip</strong> address of the server. Then it will work! </p>

<p>If you want to learn more why this happens I recommend reading <a href="http://superuser.com/questions/196893/difference-between-localhost-and-the-ip-address">this post</a>
which explains in deep the differences between <code>localhost/127.0.0.1</code> and the <code>internal ip</code> of a machine, which are falsely considered to be the same thing, but in fact they are not.</p>

python sockets networking lan