Hướng dẫn python multi client chat server - máy chủ trò chuyện đa ứng dụng khách python
Đây là một máy chủ trò chuyện đa cấp đơn giản sử dụng Máy chủ yêu cầu tên người dùng khi người dùng muốn tham gia phòng chat và chỉ chấp nhận kết nối nếu tên người dùng là duy nhất.Sau đó, nó phát thông điệp từ một máy khách đến tất cả các máy khách khác được kết nối.Cũng thông báo về mục nhập/thoát của bất kỳ khách hàng. Chạy lệnh sau trong thiết bị đầu cuối của bạn để lưu kho lưu trữ trong hệ thống của bạn $ Git Clone https://github.com/rishija/python_chatserver.git ChạyKhi bạn ở trong thư mục trong đó tệp Người phục vụ
Khách hàng
Thí dụĐối với máy chủ và máy khách đang chạy trên cùng một hệ thống Người phục vụ
SERVER WORKING Client (127.0.0.1, 51638) connected [ tesla ] Client (127.0.0.1, 51641) connected [ albert ] Client (127.0.0.1, 51641) is offline [ albert ] Khách hàng
CREATING NEW ID: Enter username: tesla Welcome to chat room. Enter 'tata' anytime to exit You: Hello albert joined the conversation albert: world albert left the conversation You: Thí dụ
CREATING NEW ID: Enter username: albert Welcome to chat room. Enter 'tata' anytime to exit You: World You: tata DISCONNECTED!! README: A. Code Description : The program uses multi threading concept. There are two scripts , one running on the server side , the other on the client side as it is with every chat server. The server relays the messages. The server side script accepts connections and creates ?2 threads ? for every client that connects (i) To process and respond to the who , wholast , send , broadcast commands to the client (ii) To keep sending the messages that are destined to the client. Similarly the client uses two threads , one for prompting commands from commands from user and getting inputs. The other thread receives chat messages from the server and displays them. Note : The responses for the commands are received in thread1 Each pair of thread is connected by a separate socket. (2 sockets are used per client-server) One socket between server and client is exclusively used for the server to get the messages from the client?s message queue and send it to the client thread that receives the chat messages. Shared queues are used. When a connection occurs , a queue is added to the set of queues with the index being the file descriptor of the connection. Hence every client has a separate index and therefore a separate queue in the server. All queues belong to the set sendqueue. A message intended for the client is put into the respective queue and the other thread is responsible for taking these messages out of the queue and send it to the appropriate client. Every queue access is wrapped up in a mutex lock to prevent clashes or race condition Common lists are used to store the information of online users , blocked users , offline users and mapping between username and the file descriptor to access the sendqueue. B. Development environment : Mac OS X Bash shell Python 3 Editor used : Textpad C. How to run the code ? There are only two scripts. Place them on the different systems. If same systems use 127.0.0.1 as the address. Server is invoked from the terminal as Python server.py |