[Java chat room server] One of the first practices

Source: Internet
Author: User

Preface

To learn any technology that is a little difficult, you must make a rational analysis of it, and then make a decisive decision-> that is, what people often say is "Seeking for profit "; although this series involves socket-related knowledge, it is a kind of state of mind that I want to share with the programmers before learning: learning is a step-by-step process, and the mentality should be adjusted at any time, keep your defense against arrogance and rashness. For example, I recently read Netease open course MIT "Introduction to algorithms". The teacher mentioned that before learning algorithms, I need knowledge in computer mathematics, discrete mathematics, probability theory, and other courses, so programmers who have been learning poor algorithms may wish to start with the basics. This is a curse caused by Chinese-style education! (The words 10 thousand are omitted here ......)


Original article address: Building a Java chart server [1]

Project source code: Socket_Chat

Directory [Java chat room server] One of the first practices (completed) [Java chat room server] Practice 2 listening class (completed) [Java chat room server] Practice 3 receiving loop (completed) [Java chat room server] Practice-4 threads (unfinished) [Java chat room server] Practice-5 read/write cycles (server) (unfinished) [Java chatting room server] Practice 6 remove dead links (unfinished) [Java chatting room server] Practice 7 client class (unfinished) [Java chatting room server] Practice 7 read/write loop (client) (unfinished)

Body
In this tutorial, we will create a chat room system that contains a simple server and client. This article is suitable for beginners who have some network programming experience or do not have it ). Even if you have some network programming experience or have no experience, we will continue to detail the network, multithreading and other knowledge, so that you can fully understand this tutorial.
-------------------------------------------------------------------------
What does the server do?

In this tutorial, you will create a simple, centralized management and connection-oriented Java Server. In the process of doing this, you will learn some basic framework knowledge and use proven technologies to create similar servers, in this way, your server will perform well in various situations. We will also check the defects of the framework and continue to explore relevant things.

-------------------------------------------------------------------------

What is a connection-oriented server?
Generally, any server provides a centralized service during work. However, there are many different service methods and many different communication structure methods. (What should we do ?)
The chat room describes connection-oriented services in general, because a user establishes and maintains a connection and sends and receives information during the session.
This is different from the Web. The Web protocol (at least theoretically) is based on transactions-the browser requests a page, the server returns it, and the connection is closed. (In fact, the connection remains open and reusable, but this is an analogy that is more inclined to Speed Optimization than structure)
We will create a simple connection-oriented server. Learning the basic framework helps you create a better connection-oriented server.

-------------------------------------------------------------------------

Why is it created from scratch?

When creating the original server, we will use each basic Java package. This allows us to explore the underlying architecture of Server programming in Java.


Of course there are a lot of systems that focus on the available network details. In many systems, the best practical solution is to use an existing framework because it often provides useful functions such as fault tolerance, load balancing, and call lines.


Even so, the key to understanding how the framework works is the underlying (principle ). There is no existing solution that perfectly solves all problems. The existing solution often has flaws, and you must be with it. Weigh the essential qualities of various technologies from a critical perspective and select the right pre-packaging solution.

-------------------------------------------------------------------------

Why is "framework "?

The term "framework" has a technical definition in the object-oriented community-a reusable design architecture.


This traditional code reuse is different. writing code for one purpose is reused by another purpose (or with no special intent ). More accurately, a framework is reusable and can be implemented in the primary structure of special programs.


One idea between framework and traditional code reuse is that traditional reuse tells you how to insert pre-existing components in your special architecture, A framework inserts special components in a pre-existing architecture.


The code in this tutorial is not a technical architecture, because it is not enough for you to simply reuse it in other projects. Building the above architecture is beyond the purpose of this tutorial. You need to explore basic Server programming technologies in depth.


However, at the design level, it is an architecture because the code of the framework will be used again and again.

-------------------------------------------------------------------------

Our framework

The architecture used in this tutorial has seven functional points. Only after you complete this tutorial will you be familiar with these seven features. When you create your own connection-oriented server, you can use them again.


List of seven function points:


Listener receive loop thread read/write loop (server) remove dead link client read/write loop (client)

Conclusion
The first article gives everyone a general understanding of the system, from a knowledge point to a list of system functions, is enough to attract every programmer.
References

[1]. Building a Java chart server

[2]. Java sockets 101 and Chinese Series JAVA Socket (Socket) 101

[3]. Basic principles of Java socket Communication

Related Article

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.