# Tcp Chat server
import socket, select
def broadcast_data (sock, message):
    for socket in CONNECTION_LIST:
        if socket != server_socket and socket != sock :
            try :
                socket.send(message)
            except :
                socket.close()
                CONNECTION_LIST.remove(socket)

if __name__ == "__main__":
    CONNECTION_LIST = []
    RECV_BUFFER = 2048
    PORT = 2300
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    HOST = socket.gethostbyname('0.0.0.0')
    server_socket.bind((HOST, PORT))
    server_socket.listen(15)
    CONNECTION_LIST.append(server_socket)
    print(socket.gethostbyname_ex(HOST)[0], PORT)
 
    while 1:
        read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])
        for sock in read_sockets:
            if sock == server_socket:
                sockfd, addr = server_socket.accept()
                CONNECTION_LIST.append(sockfd)
                print "Client (%s: %s) connected" % addr
                try:
                    client = sock.recv(1024)
                except:
                    continue
                broadcast_data(sockfd, "[%s: %s: %s] entered room\n" % client, addr)
             
            #some incoming message from a client
            else:
                try:
                    data = sock.recv(RECV_BUFFER)
                    if data:
                        broadcast_data(sock, "\r" + '<' + str(sock.getpeername()) + '> ' + data)
                 
                except:
                    broadcast_data(sock, "Client (%s, %s) is offline" % addr)
                    print ("Client (%s, %s) is offline" % addr)
                    sock.close()
                    CONNECTION_LIST.remove(sock)
                    continue
    server_socket.close()
