Use Java to build your own chat room software step by step (first) (yinyin)

Source: Internet
Author: User

To:
Http://www.javatwo.net/JavaPaper/java_chat_room.pdf

Theme
------------
Blueprint/the plan:
We will use Java to create an online chat program. This file will be simple
This section introduces what Java is and outlines the structure and operation mode of the program.

Coffee cup
0.0.0
Java is a programming language launched by Sun Microsystem in 1995. It has many features that are not available in traditional languages.
0.0.1
Apart from some inherent advantages of Java, his biggest charm lies in its application programming interface (API, Application Programming Interface). What is API? The so-called API refers to the form call or data structure provided by the job environment to the program designer to call the system, link library, or component. In other words, it is the interface that the operating system or operating environment provides to the application and system core communication. For example, printf specified in ansic is a function call used to output text to the standard. Therefore, you can easily design Java programs as long as you know which classes Java provides for use.
0.0.2
The Java language has evolved from sun to today and has become a very large software environment. You can
Get the latest API Spec from http://java.sun.com as a reference for writing Java programs.
Yes, because Java is a completely object-oriented language, its API is actually a huge category library, and the correspondence that communicates with the system is encapsulated in various categories by category.
0.0.3
Note: Java is not only a set of software, but a specification. There are many vendors who can propose their own Java products based on this specification. program designers design their programs for the Java platform. What they write is neither a Windows program nor a UNIX program, it is a Java program. Java has been developed to version 1.4.1. Currently, IBM and Sun Microsystem are mainly used to implement the Java System Environment on personal computers.
0.0.4
The Java specification can be divided into two parts: the specification of the language itself and the API of the Java platform, that is, the content of the built-in category library. These two parts have been evolving, and the class library expansion is quite fast. This can be seen from the size of JDK: 10 MB of Version 1.1 to 36 MB of version 1.4.1, the expansion of the language itself is not so rapid: It is expected that model will be supported in version 1.5.
0.0.5
Without a doubt, the development of Java will become more and more interesting.

Birds in the blue sky
0.1.0
Here's a brief introduction to the network: connecting electronic support facilities through cables, optical fiber cables, or radio networks so that they can exchange information through the transmission media. Then how does the information pass from us to the other end? In short, we can explain from the following table:

"Application applications send data via a letter provided by the call application design interface. The Transport Protocol Driver accepts requests from the application and packs the requests to the network protocol driver. ask the driver to send the data to the specified address. The network protocol driver attaches the information sent by the transport protocol driver to the recipient address and then delivers the information to the network device driver. The network device driver must control the hardware of the network device and send the information. Network Device Drivers are directly related to network hardware devices (such as Ethernet cards) and communicate directly with hardware devices.

A network hard device receives commands from the device driver and sends the data to the transmission media header.
This part may be an Ethernet card, a Mark ring network card, a bluetooth device, a serial port, or a radio
And so on. Transmit media signals to run on this layer. This part may be twisted pair wires, coaxial cables,
Optical fiber, water, air, or vacuum. Collect and organize signals from the transmission media
Then notify the driver: "Hello! Please pay when your goods arrive .」

The Network Device Driver sends the information to the network protocol driver after receiving the notification from the network hardware device and then sorts and checks the information. After the network protocol driver obtains the information sent by the network device, the network protocol driver will check whether the information is to be handed over to him. If not, the information will be lost, otherwise, it will be handed over to the transport protocol driver. The transport protocol driver receives the information sent by the network protocol driver, sorts out and checks the information, and then delivers the information to the application. Application applications obtain information by calling the application design interface. "

0.1.1
Currently, Java only recognizes the network type of IP (Internet Protocol) and TCP and UDP.
Transmission communication protocol. The Internet uses the IP network communication protocol.
0.1.2
There are some differences between TCP and UDP. The three biggest differences are that TCP will ensure that the data is absolutely sent. If the data is not sent, the application will be notified, whereas UDP does not, the information sent is like a broken kite. TCP also ensures that the order after receiving the data will be the same as that during transmission, that is, if your program sends the package a first and then sends the Package B, the program at the other end will first receive the Package A before it receives the Package B, while UDP does not provide such protection. Because TCP provides various guarantees for data transmission, TCP imposes a higher load on the network than UDP, and the speed is naturally slower.

0.1.3
Most programs use TCP/IP as the communication protocol, which saves the trouble of checking data integrity.
If your program does not need these two features, you can consider using UDP/IP as the network data transmission medium for your program.
0.1.4
Basically, because application interfaces have been packaged, accessing data through the network is like reading and writing files,
It is quite simple.
0.1.5
Back to the Java language itself, the following lists some common terms and general descriptions:

Class ):
==============
It is a collection of data domain bits and methods used to process the data. A category is declared using the keyword class with the access level of the category, the name of the category, and the Inheritance status of the category.
To generate an object based on a certain category, you must use the keyword new and add the expected category name and construction parameters. For the generated object, see object) for example, button STR = new button ("OK ");
Case (Instance) or STR is an object of the button category.

Inheritance:
======
JAVA supports Object Inheritance. to inherit a class, you only need to use extends when declaring this new class.
Add the expected category name to the keyword.

Abstract method ):
======================================
Abstract methods are pure virtual functions in C ++. To declare an abstract method, you only need to add
Abstract The keyword.

Abstract class ):
================================
If a category has more than one abstract method, it is an abstract category. Abstract categories must be advertised with the keyword abstract. Because the abstract classes are incomplete, you cannot generate object entities of the abstract classes.

Interface ):
==================
To avoid ambiguity when a class inherits more than two classes at the same time, Java,
Only one category can inherit from one category. However, this is inconvenient to make a category have multiple features. The interface was born to make up for this. Only constants and abstract methods can be declared in the interface to avoid name conflicts. To implement an interface, you must use the keyword implements to declare a new category.

Deprecated ):
====================
In the process of Java evolution, some standard methods will be renamed or discarded for some reason. To maintain forward compatibility, most of these methods will remain in the standard suite, but it is not recommended that new programs use them. The API specification lists alternative new methods or solutions next to discarded methods.

0.1.6
For the structure and syntax of the Java language, refer to the reference book or the Java
Language Specification.

Charcoal pencil
0.2.0
Okay. Let's see what the chatroom program will look like?
Below is the interface that I think:

0.2.1
How can we transmit the information? There are many ways to do this: Star, tree, or ring.
Because the star architecture is easier to implement and manage, we will adopt this structure.
The star architecture is as follows:

0.2.2
So what will the server and user-side programs say to achieve the purpose of making the user chat? The following is what I think of. Of course, you can also have one set of yours: first, end users connect to the server and say to the server:
「 Early, I am .」 Then, as long as there is a message sent to the server, the server will send this message to all people who are online with it, including those who send the message.

0.2.3
In other words, we manage the content management right of the user's display information window to the server, so that all the Members in the chat room can see the same content. 1. Connect the user-side program
The first sentence of a program on the server end is to indicate its identity. Each
To all the users on the server. If the user hears the service
When the client is talking, it posts the server's speech to the message window and displays it to the user.

0.2.4
Based on the above, we will write two sets of Programs: User-side programs and server-side programs. Integer
Chat rooms can run smoothly only when two groups of programs run simultaneously. The user-End program
There will be beautiful graphical user interfaces (guis) to facilitate terminal user operations. As for the server-side Process
After all, it is the core of the entire system and cannot have any errors.

The blueprint ends.
(Continued)

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.