0

I am new to python and I am using socketserver to try to create a server that broadcasts all the received message from one client to all the connected clients, but facing a problem. I get the following error in the end:

Exception happened during processing of request from ('127.0.0.1', 14872) Traceback (most recent call last): File "C:\Users\umair\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py", line 628, in process_request_thread self.finish_request(request, client_address) File "C:\Users\umair\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py", line 357, in finish_request self.RequestHandlerClass(request, client_address, self) File "C:\Users\umair\AppData\Local\Programs\Python\Python35-32\lib\socketserver.py", line 684, in init self.handle() File "C:\Users\umair\Desktop\socketserverthread_server.py", line 52, in handle clients.send(data) OSError: [WinError 10038] An operation was attempted on something that is not a socket

My server code is as follows:

import socketserver
import threading


host = '127.0.0.1'
port = 6666

all_clients = []

class ThreadingHandler (socketserver.BaseRequestHandler):

    def handle(self):
        if (self.request) not in all_clients:
            all_clients.append(self.request)
        data = self.request.recv(1024)
        print('%s writes: ' % str(self.client_address), end = " ")
        print(data.decode())
        for clients in all_clients:
            clients.send(data)

class ThreadingServer (socketserver.ThreadingMixIn, socketserver.TCPServer):
    pass


myserv = ThreadingServer((host, port), ThreadingHandler)

t = threading.Thread(target = myserv.serve_forever)
t.setDaemon(True)
t.start()

print('The server is online')

The client code is:

import socket

host = '127.0.0.1'
port = 6666


while True:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    data = input('You: ')
    sock.send(data.encode())
    received = sock.recv(1024)
    print('Received: ', received.decode())

I am running two of these client codes to test it. The first message from a client doesn't get broadcast, and is just received by the same client. On sending the second message, I receive the above mentioned error. Is it the problem due to the fact that I am creating a new socket in every cycle? I have to create the socket in the loop because if I don't, then I cannot send and receive continuously. I think the socket object is destroyed after a request and a response. I have no idea what's going on. So any help is appreciated. Thank you.

Umair47
  • 1,102
  • 1
  • 8
  • 12

1 Answers1

0

On sending the second message, I receive the above mentioned error. Is it the problem due to the fact that I am creating a new socket in every cycle? … I think the socket object is destroyed after a request and a response. I have no idea what's going on.

You do have an idea what's going on; indeed the client's socket object sock from a previous loop cycle is destroyed at the time when the new socket is assigned to the variable, which causes the old socket to be closed. But independently thereof the server already closes its request socket due to the use of socketserver, just after one request has been handled, i. e. after your handle(self) returns. The above mentioned error arises from the omission to take this closed socket object in all_clients into account, which could be done e. g. so:

        for clients in all_clients[:]:
            if clients._closed: all_clients.remove(clients)
            else:               clients.send(data)

The first message from a client doesn't get broadcast, and is just received by the same client.

The message does get broadcast (sent to each client), it's just that the other client doesn't bother about receiving it, because it is waiting in input('You: ').

I have to create the socket in the loop because if I don't, then I cannot send and receive continuously.

That's only true in a sense because you use socketserver, which closes the connection after each request - not quite helpful in case of a chat server.
See the question "Handle multiple requests with select" for an example server without the hassle brought by socketserver et al.

Armali
  • 18,255
  • 14
  • 57
  • 171