1

Hi i have an exercise to build with sockets select and msvcrt(btw using python 2.7), server and clients of mltiplie chat(means that the server and the clients need to be built non-blocking) that every client will send message and the server will send the message to all the clients except the one who send it, this is the server:

import socket
import select


IP = "192.168.1.154"
port = 123
default_buffer_size = 1024
open_client_sockets = []
messages_to_send = []


def send_waiting_messages(wlist):

    for message in messages_to_send:
        (client_sock, data) = message
        if client_sock in wlist:
            for sock in open_client_sockets:
                if sock is not client_sock:
                    sock.send(data)
            messages_to_send.remove(message)


def main():

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind((IP, port))
    sock.listen(5)
    print("The server is on and waiting for client...")
    while True:
        rlist, wlist, xlist = select.select([sock] + open_client_sockets, open_client_sockets, [])
        for current_socket in rlist:
            if current_socket is sock:
                (new_socket, addr) = sock.accept()
                open_client_sockets.append(new_socket)
            else:
                data = current_socket.recv(default_buffer_size)
                if data == "":
                    open_client_sockets.remove(current_socket)
                    print("Connection with client closed")
                else:
                    messages_to_send.append((current_socket, 'Hello ' + data))
        send_waiting_messages(wlist)


if __name__ == '__main__':
    main()

Building the server wasnt hard because it was guided(if it was not guided i would never got this code working) by the book but i have problem building the client and the main reason is that i dont understand how select.select works, couldn't find answer that will simplify enough this module for me. this is what i did with the client:

import socket
import select
import msvcrt

IP = "192.168.1.154"
port = 123


sockets = []


def write():
    pass


def main():
    sock = socket.socket()
    sock.connect((IP, port))
    while True:
        rlist, wlist, xlist = select.select(sockets, sockets, [])
        for current_socket in rlist:
            if current_socket is sock:
                data = current_socket.recv(1024)
                print(data)
            else:
                sockets.append(current_socket)
        write()


if __name__ == '__main__':
    main()

This probably shows you that I have low understanding of the module select and the exercise actually. I saw some threads that has similar question but I understand nothing from them so I realy need good explantion. In conclusion I realy am lost...

Jonathan Levy
  • 57
  • 1
  • 5

1 Answers1

1

select needs (on Windows) at least one non-empty list of ‘waitable objects’, thus with sockets = [] it cannot work. Set sockets = [sock] after sock = socket.socket().

For a simple, but complete example see this answer to "Handle multiple requests with select".

Armali
  • 18,255
  • 14
  • 57
  • 171