简体   繁体   中英

Python unix socket multithread [Errno 106]

I have a socket server script running in a file that accepts the connection then stores it in an array with other parameters recieved from the client.

The server will recieve lots of data from another socket server and it will be kind of a distributor. I would like to start multiple threads that listens to the server(it sends data based on what they sent in the beginning).

When I tested the clients with 2 separate py files everything worked alright, but now I tested with a multi threaded approach and it will only let the first thread connect. The error message is: [Errno 106] Transport endpoint is already connected.

So as I think it there is only 1 socket allowed between 2 processes, but for my approach it would be really great to treat them separately and send them "push notification". Is there any way to create the connections separately so I coudl simply loop trough a list and make a bunch of threads that all listens to different data from the server?

Probably it does not help, but here are the codes: This is how I start the server:

ServerSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

try:

    ServerSocket.bind('./uds_socket')
except socket.error as e:
    print(str(e))

ServerSocket.listen(10)

And this is the client:

ClientSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

def client():
    try:
        ClientSocket.connect('./uds_socket')
    except socket.error as e:
        print(str(e))
    
    ClientSocket.recv(1024)
    msg = "answer"

    #After the connection is done it will recieve a message from the server and send back the id's of the data it needs.

    ClientSocket.send(str.encode(msg))

    while True:
        Response = ClientSocket.recv(1024)
        print(Response.decode('utf-8'))
    
    ClientSocket.close()

edit: Barmar pointed out that on the client side the ClientSocket should called each time. I moved it to the function and it works fine.

This error might happen because you haven't closed the connection and is trying to connect again, thus erroring out to 106

import socket


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'localhost'
port = 5000

# First connection
result = s.connect_ex((host, port))
print(f'First connection. The result is {result}')

# Second connection
result = s.connect_ex((host, port))
print(f'Second connection. The result is {result}')

To avoid that make sure you are not trying to reconnect to the same host and port. Your script session (thread) saves the connection state.

Solution

s.close()

PS: You need some active host in order to connect, otherwise, you get a 111 error.

Fast API Server

First install the packages necessary, and their dependencies.

pip install fastapi
pip install uvicorn

Then, create a script as shown below in order to have some active TCP/IP connection.

from fastapi import FastAPI
import uvicorn


app = FastAPI()


@app.get('/')
async def home():
    return 'NOPE'


if __name__ == '__main__':
    uvicorn.run('main:app', host='localhost', port=5000, reload=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM