Python Forum
WinError 10061 with socket - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: WinError 10061 with socket (/thread-43876.html)



WinError 10061 with socket - absolut - Jan-13-2025

Hi,
I'm trying to create a socket with python:
Mi server.py:
import socket

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('', 8089))
serversocket.listen(5) # become a server socket, maximum 5 connections

while True:
    connection, address = serversocket.accept()
    buf = connection.recv(64)
    if len(buf) > 0:
        print (buf)
        break
My client:

import socket

print("Client calling .....")

socket_main = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_main.connect(('192.168.1.171', 8089))

while True:
    message = 'Hello'
    socket_main.send(message.encode())
I have disabled the firewall, but it says me:

Traceback (most recent call last):
  File "C:\script\client.py", line 6, in <module>
    socket_main.connect(('192.168.1.171', 8089))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Any help? Thanks


RE: WinError 10061 with socket - Gribouillis - Jan-13-2025

This is probably due to the break statement in your server code. Here is a modified code that works for me
# server code
import socket

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # allows reuse of the same port if the server script restarts
serversocket.bind(('', 8089))
serversocket.listen(5) # become a server socket, maximum 5 connections

while True:
    connection, address = serversocket.accept()
    try:
        buf = connection.recv(64)
        if buf:
            print (buf)
    finally:
        connection.close()
and the client code (on the same host)
import socket

print("Client calling .....")

socket_main = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    socket_main.connect(('', 8089))
except ConnectionRefusedError:
    print('Cannot connect to server')
else:
    while True:
        message = 'Hello'
        try:
            socket_main.send(message.encode())
        except ConnectionResetError:
            break
Are you server and client code on different hosts? I would not recommend disabling the firewall...


RE: WinError 10061 with socket - absolut - Jan-13-2025

(Jan-13-2025, 09:12 AM)Gribouillis Wrote: This is probably due to the break statement in your server code. Here is a modified code that works for me
# server code
import socket

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # allows reuse of the same port if the server script restarts
serversocket.bind(('', 8089))
serversocket.listen(5) # become a server socket, maximum 5 connections

while True:
    connection, address = serversocket.accept()
    try:
        buf = connection.recv(64)
        if buf:
            print (buf)
    finally:
        connection.close()
and the client code (on the same host)
import socket

print("Client calling .....")

socket_main = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    socket_main.connect(('', 8089))
except ConnectionRefusedError:
    print('Cannot connect to server')
else:
    while True:
        message = 'Hello'
        try:
            socket_main.send(message.encode())
        except ConnectionResetError:
            break
Are you server and client code on different hosts? I would not recommend disabling the firewall...

With your code:
========================= RESTART: C:\script\client.py =========================
Client calling .....
Traceback (most recent call last):
  File "C:\script\client.py", line 7, in <module>
    socket_main.connect(('', 8089))
OSError: [WinError 10049] The requested address is not valid in its context



RE: WinError 10061 with socket - Gribouillis - Jan-13-2025

(Jan-13-2025, 09:25 AM)absolut Wrote: The requested address is not valid in its context
I don't know your exact machine settings and I don't use Windows. Try to fix the host addresses in my code. Instead of '', try to use 'localhost' or '127.0.0.1' or '0.0.0.0' or '192.168.1.171' as you did before.


RE: WinError 10061 with socket - absolut - Jan-13-2025

(Jan-13-2025, 10:05 AM)Gribouillis Wrote:
(Jan-13-2025, 09:25 AM)absolut Wrote: The requested address is not valid in its context
I don't know your exact machine settings and I don't use Windows. Try to fix the host addresses in my code. Instead of '', try to use 'localhost' or '127.0.0.1' or '0.0.0.0' or '192.168.1.171' as you did before.

it works! Thanks