- Version: February 2023
To demonstrate using advanced Java features along with good object-oriented design to create a simple Client-Server JSON protocol. The goal of which is to Demonstrate a modular way of approaching Java server protocols.
- Please run
gradle Serverandgradle Clienttogether.
By default, the server will run on port 8888.
And the Client will connect to localhost on port 8888.
To change the port, use the -Pport=<int> flag.
To change the host, use the -Phost=<string> flag.
- For example,
gradle Server -Pport=9999will run the server on port 9999. - For example,
gradle Client -Phost=localhost -Pport=9999will connect to the server on port 9999.
The protocol is a simple JSON protocol.
In order to implement the protocol, you must send
an integer containing the length of the JSON string you
are sending. (4 bytes)
Then you must send the JSON string itself.
{
"operation": 1
}operationis the operation ID you wish the server to perform. (Integer)- See below for the full list of Protocols.
1- "Hypotenuse", returns the hypotenuse of a right triangle.
{
"operation": 0
}0- "Shutdown", shuts down the connection between the client and server.
{
"operation": 1,
"a": 5,
"b": 10.5
}operationRepresents the operation ID. (1)aRepresents the first side of the triangle. (Number)bRepresents the second side of the triangle. (Number)
{
"operation": 1,
"a": 5,
"b": 10.5,
"result": 11.629703349613008
}operationRepresents the operation ID. (1)aRepresents the first side of the triangle. (Number)bRepresents the second side of the triangle. (Number)resultRepresents the result of the operation. (Double)
{
"error": -1,
"message": "Error Message"
}errorRepresents the error code. (Integer)messageRepresents the error message. (String)
- -1 - Internal Server/Client Error
- This error is used when the server or client encounters a java error.
- 0 - Malformed Json
- This error is used when the server or client receives malformed JSON string.
- 1 - Unsupported common.Operation
- This error is used when the server or client receives an unsupported operation ID.
- 2 - Illegal Argument Type
- This error is used when the server or client receives an argument of the wrong type.
- 3 - Missing Required Argument Type
- This error is used when the server or client receives a missing argument.