A Java simulation of the token ring algorithm for distributed mutual exclusion. A token passes between clients over UDP, and only the client that holds the token may send data to the server.
Server/TokenServer.javalistens on UDP port 8000 and prints every message it receives.Client/Client.javaruns a client. When it holds the token it asks whether you want to enter data, sends it to the server, then passes the token on.Client/TokenClient.javaandServer/Server.javahold the UDP send and receive logic.
The report (Ahmed217380-Assignment1.docx) and the screenshots in this repository show a sample run.
JDK 17 and Maven 3.9.
cd Token-Ring_Mutual_Exclusion_Algorithm_Simulation
mvn compile
# terminal 1: the server
java -Dfile.encoding=UTF-8 -cp target/classes Server.TokenServer
# terminal 2: a client
java -Dfile.encoding=UTF-8 -cp target/classes Client.ClientIn the client, answer yes to send a line of text to the server, or no to pass the token on. After no the client waits for the token to come back on UDP port 8002. To try it by hand, send the text Token to that port from another program. The generated TokenRing_Mutual_Exclusion_Algorithm_Simulation class only prints "Hello World!" and is not the entry point.
Checked with the server and one client: a line typed after yes arrived at the server, and after no the client received the Token and went back to the prompt.
- The client created a new input reader for every question, which lost typed text and sent "null" to the server. It now uses one shared reader.
- The packet length was counted in characters instead of bytes, so text with non-ASCII characters was cut off. It now uses the byte length and UTF-8.
- Closing the input crashed the client with a NullPointerException. It now stops cleanly.
MIT. See LICENSE.