13.3.2 Guess number Games
The following example is a console game for guessing numbers. The rule of the game is: when the client first connects to the server side, the server side produces a " Random numbers between the 0,50", then the client input numbers to guess the number, each time the client input number, sent to the server side, the server to determine the client sent the number and the relationship between the random number, and feedback comparison results, the client has a total of 5 guesses the opportunity to guess when the prompt guess, when the input" quit " When you end the program.
Similar to the previous example, in the development of a network program, first need to break down the implementation of the function, feel that the function is implemented in the client program or in the server-side program. The distinguishing rule is: The client program realizes the interface function of receiving user input, and realizes some basic verification to reduce the pressure of the server, while the logic of the program core and the function of data storage are implemented on the server side. The client and server-side features that follow this principle are shown below.
Client program features list:
1. Receive user console input
2, to determine whether the input content is legal
3, in accordance with the protocol format to send data
4, according to the server side feedback to give the corresponding prompts
Server-side program features list:
1. Receive Client send data
2, according to the protocol format to resolve data
3, to determine the number of sent over and the relationship between random numbers
4, according to the results of the production agreement data
5, the production of data feedback to the client
In this example, the actual network command used is only two, so the protocol appears to be relatively easy to format.
Where the client program protocol format is as follows:
1. Converts the number entered by the user to a string and converts it to a byte array
2, send "quit" string representative exit