Tic Tac Toe Server Example

Written in Python, this was created for the last assignment of Networking Concepts in Year 1. There are 2 other files player1.py and player2.py which interact with server.py. The player files contain the game board and main game loop. This works over a local network and allows players to play Tic Tac Toe on different computers.

Server.py

import socket, select

#function to broadcast chat messages to all connected clients
def broadcast_data (sock, message):
    #do not send the message to master socket and the client who has sent the message
    for socket in CONNECTION_LIST:
        #for each socket in the connection list do this:
        if socket != server_socket and socket != sock:      #if the socket is not equal to the server socket and the socket that sent the message, do this:
            try:
                socket.send(message)        #send the message to the other socket
            except:
                #broken socket connection may be, chat client pressed ctrl+c for example
                socket.close()      #close the connection and remove it from the list of connected sockets
                CONNECTION_LIST.remove(socket)
if __name__== "__main__":

    #list to keep track of socket descriptors
    CONNECTION_LIST = []
    RECV_BUFFER = 4096  #data buffer size
    PORT = 5004         #port number

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)       #create a stream socket called server_socket
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind(('', PORT))      #bind port number to the address, left empty so it can be any address the server is on
    server_socket.setblocking(0)        #set the port to non-blocking so it can listen for other connections and talk to them instead of waiting for one client
    server_socket.listen(10)            #keep listening for connections, allow up to 10

    #add server socket to list or readable connections
    CONNECTION_LIST.append(server_socket)
    print socket.gethostname()      #prints the name of the host the server is on
    print "Tic Tac Toe server started in port " + str(PORT)
    while 1:
        #get the list sockets which are ready to be read through select
        read_sockets, write_sockets, error_sockets = select.select(CONNECTION_LIST,[],[])
        for sock in read_sockets:       #for every sock in read_sockets do this:
            #new connection
            if sock == server_socket:
                #if the incoming connection has the same details, accept the client and add it's address to the connection list, printing that it has connected
                sockfd, addr = server_socket.accept()           #accept the connection
                CONNECTION_LIST.append(sockfd)                  #push back connection details into the list
                print "Client (%s, %s) connected" % addr

            #some incoming message from a client
            else:
                try:
                    #get the incoming data, if there is something, send it to the other clients connected to the server
                    data = sock.recv(1024)
                    if data:
                        broadcast_data(sock, data)
                except:
                    #if a client disconnects print a message saying the client has disconnected, close the associating socket and remove it from the connection list
                    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()