Implementation of the chat room application of Python socket C/S structure

Source: Internet
Author: User
PythonThe chat room application of the socket C/s structure

Service side:

#!/usr/bin/env Python#coding:utf8 Import socket,selectdef broadcast_data (sock,message): For sockets in Conn_list:if sock ET = server_socket and Socket! = Sock:try:socket.send (message) Except:socket.close () Conn_list.remove ( SOCKET) If __name__ = = "__main__": Conn_list = [] Recv_buffer = 4096 PORT = 9999 Server_socket = Socket.socket (socket.af_ INET, Socket. SOCK_STREAM) server_socket.setsockopt (socket. Sol_socket, SOCKET.  SO_REUSEADDR, 1) #端口重用, optional Server_socket.bind ((", PORT) Server_socket.listen conn_list.append (Server_socket) Print "Chat server started on port" + STR (port) while 1:read_sockets,write_sockets,error_sockets = Select.select (conn_ List,[],[]) for sock in Read_sockets: #新建连接 if sock = = server_socket:sockfd, addr = server_socket.accept () con N_list.append (SOCKFD) print "Client (%s,%s) connected"% addr Broadcast_data (SOCKFD, "[%s:%s] entered room\n"% add r) #进入聊天室 Else:try:data = SOCK.RECV (recv_buffer) if data:     Broadcast_data (sock, "\ r" + ' < ' + str (sock.getpeername ()) + ' > ' + Data ' except:broadcast_data (sock, "C Lient (%s,%s) is offline "% addr) ' print ' Client (%s,%s) is offline"% addr Sock.close () Conn_list.remove (Soc K) Continue Server_socket.close ()

Client:

#!/usr/bin/env Python#coding:utf8import Socket,select,string,sys def prompt (): Sys.stdout.write ("
 
  
   
   ) Sys.stdout.flush () if __name__ = = "__main__":  if (len (SYS.ARGV) < 3):  print ' Usage:python chat_client.py host Name Port '  sys.exit ()  HOST = sys.argv[1] Port = Int (sys.argv[2])  s = socket.socket (socket.af_inet, socket. SOCK_STREAM) S.settimeout (2)  try:  s.connect ((HOST, PORT)) except:  print ' Unable to connect '  Sys.exit ()  print ' Connected to remote host. Start sending messages ' prompt () while  1:  socket_list = [Sys.stdin, S]   read_sockets, Write_sockets, Error_sockets = Select.select (Socket_list, [], []) for   sock in Read_sockets:   if sock = = s:    data = Sock.rec V (4096)    if not data:     print ' \ndisconnected from chat server '     sys.exit ()    Else:     Sys.stdout.write (data)     prompt ()    else:    msg = Sys.stdin.readline ()    s.send (msg)    prompt ()
 
  
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.