Java Programming things 109--Network Programming Example 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.