20145207 Java programming 10th Week of study summary

Source: Internet
Author: User

Textbook Knowledge Summary

13.1 Network Overview

13.1.1 Computer network Overview

    • The essence of network programming : Data transfer between two (or more) devices, such as a computer.
    • computer network definition: Through a certain physical equipment will be in different locations of the computer connected to form a network, the network contains the devices are: computers, routers, switches and so on.
    • The main advantage of the network is sharing: sharing devices and data.
    • IP address refers to the Internet Protocol address (English: Internet Protocol address, and also translated as Internet Protocol addresses), is the abbreviation of IP address. IP address is a unified address format provided by the IP protocol, which allocates a logical address for each network on the Internet and each host to block differences in physical addresses. In the computer network, the name IP address is now defined as the IPV4 protocol, which specifies that each IP address consists of a number of 4 0-255, such as 10.0.120.34.
    • An IP address can correspond to multiple domain names, and a domain name can only correspond to one IP address.
    • DNS servers implement domain name resolution, and when DNS is not working, only the device can be accessed through an IP address. So the use of IP address is more common than the domain name.
    • Each program in the same computer corresponds to a unique port, so that the data sent to each port can be differentiated on a single computer, in other words, multiple network programs can be run concurrently on a single computer without interfering with each other.

13.1.2 Network Programming Overview

    • In network communication, the first program to initiate the communication is called the client program, which is called the client, and the program waiting for the connection in the first communication is referred to as the server side (server) program, or server. Once the communication is established, the client and server side are exactly the same, without the essential difference.
    • Use C/S structure (client/server): The client and server side should be developed separately at development time. Advantages: The client, the server is specially developed, the expressive force is rich. Disadvantage: Poor versatility, almost universal, etc., the maintenance of a larger pressure.
    • Use b/S Architecture (Browser/server): Only development server-side is required at development time. Advantage: Less pressure on development, no need to maintain client. Disadvantage: The browser limit is relatively large, the performance is not strong, can not carry out system-level operation and so on.
    • Peer program Point-to-point: A special program that should include both client programs and server-side programs, such as BT, using the client program to connect the other seeds (server side), and using the server side to transfer data to other BT clients.
    • Protocol (Protocol): In the actual data exchange, in order to let the receiver understand the data, then you need to specify the format of the data, the format of the data is the protocol.

13.1.3 network communication Mode

In the existing network, there are two main ways of network communication:

1. TCP (Transmission Control Protocol) mode 2, UDP (User Datagram Protocol) mode

In network communication, the TCP method is similar to the call, the use of this method of network communication, the need to establish a dedicated virtual connection, and then reliable data transmission, if it fails, the client will automatically resend the data. And the UDP way is similar to sends the text message, uses this way carries on the network communication, does not need to establish the specialized virtual connection, the transmission is not very reliable, if sends the failure the client cannot obtain.

13.2 Network Programming Technology

13.2.1 Network Programming Steps

This paper introduces the steps of network programming, which is based on C/s structure.

13.2.1.1 Client Network programming steps

1. Establish a network connection

The first step in client network programming is to establish a network connection. When establishing a network connection, you need to specify the IP address and port number of the server to which you are connected, and after completion, a virtual connection is formed, and subsequent operations can be exchanged through the connection. 2. Exchanging data (repeatable steps)

Once the connection is established, the data can be exchanged through this connection. Exchange data in strict accordance with the request response model, the client sends a request data to the server, the server feedback a response data to the client, if the client does not send the request, the server side will not respond. Depending on your logic needs, you can exchange data multiple times, but you still have to follow the request response model. 3. Close the network connection

After the data exchange is complete, shut down the network connection, release the system resources such as port, memory and so on, and end the network programming.

13.2.1.2 server-side network programming steps

1. Listening Port

The server side is a passive wait connection, so after the server is started, you do not need to initiate a connection, but you only need to listen to a fixed port on the local computer. This port is the server-side open to the client port, the server-side program runs the local computer's IP address is the server-side program IP address. 2. Get the connection

When the client connects to the server side, the server can obtain a connection that contains information about the client, such as the client IP address, and the server side and the client also exchange data through the connection. Typically in server-side programming, when you get a connection, you need to turn on a dedicated thread to handle the connection, and each connection is implemented by a separate thread. 3. Exchanging Data

The server side is exchanging data through the connections it obtains. The server-side data exchange step is to first receive the data sent by the client, then the logical processing, and then the processing of the resulting data sent to the client. In simple terms, it is the first to receive and then send, which is different from the client's data exchange sequence. In fact, the server-side connections and client connections are the same, but the data exchange steps are different. Of course, the server-side data exchange can also be done multiple times. After the data exchange is complete, the connection to the client is closed. 4. Close the connection

When the server program shuts down, the server side needs to be shut down, and the server listening port and memory can be freed by shutting down the server side, enabling the connection to be closed.

    • TCP is required to establish a connection, for the server side of the pressure is larger, and UDP is not necessary to establish a connection, the server-side pressure is relatively small.
    • Regardless of the language, the basic network programming in any way, must follow the fixed steps, after the familiar with these steps, you can do the logical processing, but still have to follow the fixed steps.

13.2.2 Java Network Programming Technology

InetAddress class: The function of this class is to represent an IP address, and the IP address and domain name-related action methods are included inside the class.

1 Note: Because the code contains an Internet URL, it requires networking when running the program, or an exception will occur.

13.2.3 TCP Programming

In the Java language, the network programming of TCP is provided with good support, in the actual implementation, the Java.net.Socket class represents the client connection, and the Java.net.ServerSocket class represents the server-side connection.

When using TCP for network programming, you need to follow the steps of the network programming described above, respectively, in the Java language of the client and server-side implementation steps.

In Client network programming , you first need to establish a connection, in the Java API, the Java.net.Socket class object represents the network connection, so establish a client network connection, that is, the creation of the socket type object, which represents the network connection, the example is as follows:

Socket SOCKET1 = new socket ("192.168.1.103", 10000);

Socket Socket2 = new socket ("www.sohu.com", 80);

In the above code, SOCKET1 implements Port 10,000th, which is connected to a computer with an IP address of 192.168.1.103, and Socket2 implements Port 80th, which is connected to the computer with the domain name www.sohu.com, and is completely transparent to the programmer as to how the underlying network is connected. If the local network is not connected when the connection is established, or if the server-side program is not turned on, an exception is thrown.

Once the connection is established, the first step in client programming is completed, and the next step is to exchange the network data according to the "request-response" model, in the Java language, the data transfer function is implemented by Java IO, which means that only the input and output streams from the connection need to be obtained. The data that needs to be sent is then written to the output stream of the connection object, and the data is read from the input stream after the send is complete. The sample code is as follows:

OutputStream OS = Socket1.getoutputstream (); Get the output stream

InputStream is = Socket1.getinputstream (); Get input stream

In the code above, the output stream and the input stream object are obtained from the connection object of Socket1, and in the whole network programming, the subsequent data exchange becomes the IO operation, which is to follow the requirements of the "request-response" model, write the data to the output stream first, and the data will be sent by the system. The server-side feedback is then read from the input stream, which completes the process of data exchange, which can be done multiple times.

Here is only the most basic output stream and input stream objects, but also based on the knowledge of the previous IO, using the flow of the nesting of these basic stream objects to the desired flow object, so as to facilitate the operation of the data.

Finally, when the data exchange is completed, the network connection is closed, the system ports and memory resources are freed, the network operation is completed, and the sample code is as follows:

Socket1.close ();

in server-side programming, because the server side of the implementation of the passive waiting for the connection, so the first step of server-side programming is to listen to the port, that is, to listen for a client connection arrives. The code to implement server-side snooping is:

ServerSocket ss = new ServerSocket (10000);

The code implements the ability to listen on port 10,000th of the current computer, and throws an exception if Port 10,000th is already occupied by another program while executing the code. Otherwise, the listener will be implemented.

The second step in server-side programming is to get a connection. The purpose of this step is to set up a socket connection object that corresponds to the client connection when a client connection arrives, freeing the client connection for server-side port usage. Realize the function just like the company's front desk, when a customer arrives at the company, will tell the front desk I look for a certain, and then the front desk to inform a certain, and then can continue to receive other customers. By obtaining a connection, the client's connection is maintained on the server side, and the server-side port is freed up to continue waiting for other client connections to be made. The code that implements the get connection is:

Socket socket = ss.accept ();

Finally, after the server-side communication is complete, close the server-side connection. The code implemented is:

Ss.close ();

    • The way of network communication in addition to the TCP way, there is a way to implement is the UDP mode. UDP (user Datagram Protocol), Chinese meaning is the User Datagram protocol, similar to texting, is a cheap way of communication, using this method without establishing a dedicated virtual connection, because there is no need to establish a dedicated connection, so the pressure on the server is much smaller than TCP , so it is also a common way of network programming.

The steps involved in UDP client programming are also 4 parts: establishing a connection, sending data, receiving data, and shutting down a connection.

Firstly, the implementation of the connection in the network programming of UDP is introduced. Where the UDP method of establishing a connection and TCP is different, only need to establish a connection object, do not need to specify the server IP and port number. The code implemented is:

Datagramsocket ds = new Datagramsocket ();

This establishes a client connection that uses the unused port number of a local computer that is randomly assigned by the system. In this connection, the server-side IP and port are not specified, so the UDP mode network connection is more like a transmitter than a specific connection.

Of course, you can create a client connection by making a port number to use for the connection.

Datagramsocket ds = new Datagramsocket (5000);

This creates a connection using port No. 5000 on the local computer. It is generally not necessary to specify a port number when establishing a client connection.

UDP mode The last step of client network programming is to close the connection. Although the UDP method does not establish a dedicated virtual connection, the connection object still needs to occupy system resources, so the connection must be closed after the use is complete. To close the connection using the Close method in the Connection object, implement the following code:

Ds.close ();

It is necessary to note that, unlike TCP, the same network connection object, UDP mode, can be sent to different server-side IP or port packets, this is not the TCP way to do.

First UDP server-side network programming needs to establish a connection, which listens to a port, the implementation of the code is:

Datagramsocket ds = new Datagramsocket (10010);

Because the server-side ports need to be fixed, the port number is typically specified when the server-side connection is established. For example, in the sample code, specify port number 10010 for the server side, and the client side connects to the server side when the port number is connected.

Then the server side begins to receive the data sent by the client, its methods of receiving and the method received by the client has been, where the function of the Receive method is similar to the TCP mode of the Accept method, the method is also a blocking method, its role is to receive data.

Network protocol

A network protocol is defined as the data format that is transmitted over a network. For the network programming beginners, there is no need to understand the TCP/IP protocol cluster, so for beginners to read the big "TCP/IP protocol" is not a very appropriate thing, because in-depth understanding of TCP/IP protocol is the network programming stage, but also in-depth network programming at the bottom of the things to do.

The network protocol is also the core of the network program, so it is necessary to design a good protocol format when the network programming is actually started.

20145207 Java programming 10th Week of study summary

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.