Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
WinError 10061 with socket
#1
Hi,
I'm trying to create a socket with python:
Mi server.py:
1
2
3
4
5
6
7
8
9
10
11
12
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:

1
2
3
4
5
6
7
8
9
10
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:

1
2
3
4
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
likes this post
Reply
#2
This is probably due to the break statement in your server code. Here is a modified code that works for me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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...
likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
(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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:
1
2
3
4
5
6
========================= 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
likes this post
Reply
#4
(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.
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 3,095 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  WinError 2, since fresh new Windows 10 install alok 1 2,419 Jan-06-2022, 11:20 PM
Last Post: lucasbazan
  pyarrow throws oserror winerror 193 1 is not a valid win32 application aupres 2 4,889 Oct-21-2020, 01:04 AM
Last Post: aupres
  WinError 87 while running .exe file Timych 0 3,397 Aug-06-2020, 02:36 PM
Last Post: Timych
  Sublime Text 3 WinError 2 File Specified HelloWorld17 0 3,386 Apr-02-2020, 09:03 AM
Last Post: HelloWorld17
  cx_freeze frozen executive introduces WinError 10049 KipCarter 3 6,955 Jan-14-2020, 02:34 PM
Last Post: KipCarter
  .py to exe error "oserror winerror 193" michael1789 3 6,217 Dec-03-2019, 05:49 AM
Last Post: michael1789
  Has anyone experience a winError[5] Access Denied in Windows 10? fstkmaro 2 16,847 Nov-11-2019, 02:38 PM
Last Post: fstkmaro
  [WinError 193] %1 is not a valid Win32 application edlipson 1 12,040 Jan-24-2018, 12:52 AM
Last Post: snippsat
  my python is popping up the error FileNotFoundError: [WinError 3] ? srinivas135 3 20,176 Sep-13-2017, 09:38 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020