A more complex example

Note: The example classes discussed in this section appear in the subdirectory advanced of the example directory.

The last example exercises most of EiffelNet's major facilities. It consists of a server that allows an arbitrary number of clients to connect to it. Each time the user of one of the client systems types a line on the keyboard, the client sends this character to the server, which then broadcasts it to all the clients (including the original sender). This scheme allows several people to talk together, hence the names chosen: the server class is called CHAT, and the client is called JOIN.

The example uses the network mode of communication, based on the NETWORK_CLIENT and NETWORK_SERVER classes. It uses automatic polling through MEDIUM_POLLER as in the previous example; the relevant command is given by class CONNECTION, an heir of POLL_COMMAND . The information exchanged between the server and its clients is described by class MESSAGE, an heir of LINKED_LIST [ [[ref:libraries/base/reference/string_8_chart|STRING]] ] similar to the earlier examples' OUR_MESSAGE (see Introduction to the examples ). Attributes include, the name client_name of the client that has sent this message, the boolean new indicating whether the current message is the first from a client that is trying to connect to the server, and over indicating that the message is the last sent by a client before disconnecting.

The server maintains a list of the currently active connections. In the receive routine, it checks on the main socket for any client trying to connect. The socket is set to be non-blocking to enable the server to continue checking the already connected clients. If the connection is successful, the server sends to the new client the list of clients already connected and adds the new connection to its list. Then it polls the connections in the list, and processes the messages, if any. If the message is tagged new, the server sends a message to all the clients indicating that a new client has joined the server; if it is tagged over, it sends a message indicating that the client has opted out.

Each client uses the MEDIUM_POLLER to check any message coming from the server and immediately displays any such message. It also checks a special connection, created with io.input as a medium, to check what the user is typing and then send it to the server. If the user types bye, the client terminates, sending a message tagged over to the server.

cached: 07/22/2024 9:44:42.000 AM