![]() ![]() # 4th arg, time_out = 0 : poll and never block # get the list sockets which are ready to be read through select Print "Chat server started on port " str(PORT) # add server socket object to the list of readable connections ![]() Server_tsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) socket(socket.AF_INET, socket.SOCK_STREAM) If any of the client socket is readable, the server would read the message, and broadcast it back to all clients except the one who send the message.If the master socket is readable, the server would accept the new connection.In the code, we're dealing with two cases: When the select function returns, the ready_to_read will be filled with an array consisting of all socket descriptors that are readable. If any of the client socket is readable then it means that one of the chat client has send a message. In the code, we set time_out = 0, and it will poll and never block.Īctually, the select() function monitors all the client sockets and the server socket for readable activity. Though the select() itself is a blocking call (it's waiting for I/O completion), we can give it a timeout. the last (normally left empty) those that we want to check for errors.the second all the sockets we might want to try writing to.the first contains all sockets that we might want to try reading.Ready_to_read, ready_to_write, in_error = \ In this chat code, the server is handling the sockets in non-blocking mode using lect() method: So, the server provides a sort of chatting room. The server broadcasts any messages from a client to the other participants. The server is like a middle man among clients. In this chapter, we'll make a chat server. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |