The example classes discussed in this section appear in the subdirectoryadvanced 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 theNETWORK_CLIENT andNETWORK_SERVER classes. It uses automatic polling throughMEDIUM_POLLER as in the previous example; the relevant command is given by class
CONNECTION
, an heir ofPOLL_COMMAND. The information exchanged between the server and its clients is described by class
MESSAGE
, an heir ofLINKED_LIST [STRING] similar to the earlier examples' OUR_MESSAGE (seesee "Object structures"). Attributes include, the nameclient_name of the client that has sent this message, the booleannew indicating whether the current message is the first from a client that is trying to connect to the server, andover indicating that the message is the last sent by a client before disconnecting.
The server maintains a list of the currently active connections. In thereceive 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 taggednew, the server sends a message to all the clients indicating that a new client has joined the server; if it is taggedover, it sends a message indicating that the client has opted out.
Each client uses theMEDIUM_POLLER to check any message coming from the server and immediately displays any such message. It also checks a special connection, created withiolinput as a medium, to check what the user is typing and then send it to the server. If the user typesbye, the client terminates, sending a message taggedover to the server.