encode / uvicorn

An ASGI web server, for Python. 🦄
https://www.uvicorn.org/
BSD 3-Clause "New" or "Revised" License
8.57k stars 745 forks source link

Error: [WinError 10054] An existing connection was forcibly closed by the remote host #2191

Closed amirfenina closed 11 months ago

amirfenina commented 11 months ago

Initial Checks

Discussion Link

no responce

Description

i honestly have copied this code from another source . and now i want to make the connection with another device thrrough ip4 adress but it keeps showing me this error any help ?

Example Code

#this is the server code 
///
import socket
from threading import Thread

SERVER_HOST = "0.0.0.0"
SERVER_PORT = 5002 
separator_token = "<SEP>" 

client_sockets = set()
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((SERVER_HOST, SERVER_PORT))
s.listen(5)
print(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}")

def listen_for_client(cs):
    while True:
        try:
            msg = cs.recv(1024).decode()
        except Exception as e:

            print(f"[!] Error: {e}")
            client_sockets.remove(cs)
        else:

            msg = msg.replace(separator_token, ": ")
        for client_socket in client_sockets:
            client_socket.send(msg.encode())

while True:

    client_socket, client_address = s.accept()
    print(f"[+] {client_address} connected."
    client_sockets.add(client_socket)
    t = Thread(target=listen_for_client, args=(client_socket,))
    t.daemon = True
    t.start()
for cs in client_sockets:
    cs.close()
s.close()
///

#this is the client's code
///
import socket
import random
from threading import Thread
from datetime import datetime

SERVER_HOST = "127.0.0.1" 
SERVER_PORT = 5002 
separator_token = "<SEP>" 
s = socket.socket()
print(f"[*] Connecting to {SERVER_HOST}:{SERVER_PORT}...")
s.connect((SERVER_HOST, SERVER_PORT))
print("[+] Connected.")
name = input("Enter your name: ")
def listen_for_messages():
    while True:
        message = s.recv(1024).decode()
        print("\n" + message)
t = Thread(target=listen_for_messages)
t.daemon = True
t.start()
///

Python, Uvicorn & OS Version

python3 vs code*

[!IMPORTANT]

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.

Fund with Polar