简体   繁体   中英

BlockingIOError: [Errno 11] Resource temporarily unavailable two clients one server sockets python

I am trying to simply send messages from two client sockets to one server socket. When I start up the server socket, it works fine, and then when I start up each of the two client sockets (I'm using 3 different terminal windows) everything works fine. If I start up the server and one client, I can send messages from the client to the server. If I then start up the second client, I can send messages from the second client to the server, however now if I go back and try to send messages from the first client to the server, I get a " BlockingIOError: [Errno 11] Resource temporarily unavailable ". What is the problem?

clientclass.py:

from itertools import count
import select
import random
import threading
import time

class Client(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.host = "127.0.0.1"
        self.port = 65432


    def run(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.id = random.randrange(0, 1000)
        print(f"client id {self.id} connected")
        self.sock.connect((self.host, self.port))
        while True:
            text = input('m: ')
            self.sock.sendall(text.encode())

server.py:

import socket
from itertools import count
import select
from _thread import start_new_thread
import threading
import time
from queue import Queue

class server(threading.Thread):
    def __init__(self):
        self.host = "127.0.0.1"
        self.port = 65432
        threading.Thread.__init__(self)

    def run(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind((self.host, self.port))
        self.sock.listen(5)
        self.sock.setblocking(0)


        print(f"server instantiated")
        inputs = [self.sock]
        outputs = []
        message_queues = {}

        while inputs:
            inputready,outputready,exceptready = select.select(inputs,outputs,inputs)
            for input_item in inputready:
                if input_item is self.sock:
                    conn, addr = self.sock.accept()
                    print("New connection from: ", addr)
                    conn.setblocking(0)
                    inputs.append(conn)
                else:
                    data = conn.recv(1024)
                    if data:
                        print("Client:  " + data.decode())
                        #message_queues[input_item].put(data)
                        if input_item not in outputs:
                            outputs.append(input_item)
                    else:
                        if input_item in outputs:
                            outputs.remove(input_item)
                        inputs.remove(input_item)
                        input_item.close()



class Message_Sender(threading.Thread):
    def __init__(self, client):
        threading.Thread.__init__(self)
        self.client = client

    def run(self):
        while True:
            text = input('m: ')
            server.conn.sendall(text.encode())

server = server()
server.start()
msg = Message_Sender(server)
msg.start()

client instances:

import clientclass

client = clientclass.Client()
client.start()

I saw your code and I think I have solved the problem you might have I changed some code In a way It receives multiple clients at the same time server.py:

import socket
from itertools import count
import select
from _thread import start_new_thread
import threading
import time
from queue import Queue

class server(threading.Thread):
    def __init__(self):
        self.host = "127.0.0.1"
        self.port = 65432
        threading.Thread.__init__(self)

    def run(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind((self.host, self.port))



        print(f"server instantiated")
        inputs = [self.sock]
        outputs = []
        message_queues = {}

        while True:
            self.sock.listen(5)
            conn, addr = self.sock.accept()
            receive = threading.Thread(target=self.receive_data, args=(conn,))
            receive.start()
            inputs.append(conn)

    def receive_data(self, conn):
        while True:
            data = conn.recv(1024)
            if data:
                print("Client:  " + data.decode())
                # do something



class Message_Sender(threading.Thread):
    def __init__(self, client):
        threading.Thread.__init__(self)
        self.client = client

    def run(self):
        while True:
            text = input('m: ')
            server.conn.sendall(text.encode())

server = server()
server.start()
msg = Message_Sender(server)
msg.start()

I have made a loop inside the run function, when someone tries to connect it will make a new thread to receive their messages. it wasn't clear to me what you wanted to do with the variables inputs and input_item, ect. but I think you can get really far with this. If you still have any problems feel free to reach out.

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