1

I am learning about the python socket library and am running into problems whenever I try to connect to the server running on my localhost with a client application.

Here is the server code:

 import socket

 HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
 PORT = 65432        # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
    print('Connected by', addr)
    while True:
        data = conn.recv(1024)
        print(data)

Here is the code for my client application:

 import socket

 HOST = "localhost" # The server's hostname or IP address
 PORT = 65432        # The port used by the server

 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
     s.connect((HOST, PORT))
     s.sendall(b'Hello, world')
     data = s.recv(1024)

 print('Received', repr(data))

Here is my error message:

 ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

Here is what I have tried so far:

  1. Disabling my Window's 10 firewall completely on the windows command prompt with the use of the following command: netsh advfirewall set allprofiles state off. This did not work

  2. To windows firewall I added an inward rule and outward rule that allows any application on my OS to access a service running on port 65432

  3. I changed my python version from 3.8.2 to 3.7.7 because before hand I was able to run this code perfectly and I was using a python 3.7 version

  4. I tried multiple different methods of setting the HOST variable, which include "localhost", '127.0.0.1', socket.gethost(), and socket.gethostbyname("localhost")

I am able to connect to the server through the use of the Window's telnet application but that is it. To be honest I have exhausted possible solutions that I can find online, and I know that this question has came up on this website a lot, but I have honestly tried every solution I have seen so far - which included three hours of searching.

I appreciate any possible help that you guys can give, thanks.

Dennis Sparrow
  • 938
  • 6
  • 13
tektek
  • 11
  • 1
  • 2

1 Answers1

0

Since the code was working earlier in the machine,this doesn't seem to be code issue. Also the code ran fine in my machine. I suggest you to run through the below steps once again:

Solution 1:
1. edit the server address as 127.0.0.1 or the host private IP in both the code just to be assured there is no discrepancy.
2. Start the server program first and make sure it didn't terminate.
3. Start the client application and check if the server program threw any error or exceptions.

Solution 2: Change the port number and follow solution 1.

Solution 3: Switch off the windows firewall from the UI just to be sure. Follow the solution 1 steps

Solution 4: Change the server address as host=''

Sreekumar
  • 151
  • 6