Java BASICS (6: Network Programming) and java Basics

Source: Internet
Author: User

Java BASICS (6: Network Programming) and java Basics
I,Unified resource positioning address (URL)

(1)Network Address

On the network, a computer is identified by a network address. There are two network address representation methods. The first representation method usually consists of four integers, for example:

166.111.4.100 indicates the home page address of a website server.

The second method is to express the network address through the domain name, for example:

Www.aaaa.edu.cn indicates the website domain name of a school.

In network programs, you can use instance objects like java.net. InetAddress to record network addresses and obtain related information. Because the access attribute of the constructor of the class java.net. InetAddress is the default mode, you cannot create an instance object by using the constructor of the class java.net. InetAddress. To create a leijava.net. InetAddress instance object, you can use its member method:

Public static InetAddress getLocalHost () throws UnknowHostException creates an Instance Object of the java.net. InetAddress class corresponding to the local computer.

(2) unified resource positioning address

The Uniform Resource location address (Uniform Resource Locator, URL) is also called the network Resource Locator in some materials. It generally points to the resources on the network. Network Resources can not only include various simple objects on the network, such as paths and files on the network, but also complex objects such as databases or search engines. A unified resource location address (URL) is usually composed of several parts, including protocols, Host, Port, and File) and Reference. The writing format and order of these parts in the uniform resource location address (URL) are usually:

Protocol: // host: Port Number/file # reference

For example: http://www.aaaa.edu.cn: 8080/demo/urlprie.html # INSTALL, where the network protocol is http, the host is www.aaaa.edu.cn, the port number is 8080, the file is demo/urlprie.html, reference is INSTALL.

 

(3) Obtain network resources through uniform resource positioning addresses

Unified resource locating address (URL) points to resources on the network. Using the java.net. URL-like member Method

Public final InputStream openStream () throws IOException. You can associate the instance object of a URL-like object with the resource it points to, so that the network resource can be treated as a special data stream. To read network resource data, follow these steps:

1) Create an instance object like a URL to point it to a given network resource;

2) Establish a URL Connection through openStream, a URL-like member method, and return a reference to the input stream object to read data;

3) optional steps: encapsulate the input stream through java. io. BufferedInputStream or java. io. BufferedReader;

4) read and process data;

5) disable the data stream.

Step 3 is an optional step. When the network is unstable or slow, you can use step 3 to increase the speed of obtaining network resource data.

 

 

II, TCP-based Network Program Design

TCP (Transmission Control Protocol) is a connection-based Protocol that can provide reliable data Transmission between computers. A metaphor is to compare data transmitted over TCP to a call, that is, to establish a connection channel and then transmit data. The two ends of the connection channel are usually called sockets ). A Socket is like an electric microphone needed for a call. After dialing the phone, you can talk to the other Party through an electric microphone or hear what the other party says. The same is true for TCP-based network communication. First, a connection is established, and then data is sent and received through a Socket.

Both parties that communicate over TCP are usually called the server and client. The server and client can be two different computers, or the same computer. The server-side program is slightly different from the client program. The specific communication program design is shown in:

UDP-Based Network Programming

User Data Protocol (UDP) is a Protocol used to control network data transmission. The transmitted data is first encapsulated in the datagram packet, and then the UDP control is used to send and receive the datagram packet. Compared with TCP, UDP does not establish a reliable connection when controlling data transmission. Generally, UDP-based data transmission is compared to sending or receiving a mail to the post office. Compared with TCP, UDP is generally considered a simple and unreliable connectionless network data transmission protocol. UDP-based network data transmission does not guarantee that the datagram packet will arrive at the specified host smoothly or that the datagram packet will arrive at the specified host in the order of sending.

In daily life, you can send or receive messages through the post office. Similarly, UDP-based network data transmission uses a Datagram Socket ). A datagram socket is a socket used to send or receive a datagram packet. Its role in data communication is similar to that in post offices. The data to be transmitted is typically encapsulated into a datagram packet, which is a bit similar to a letter.

Shows the program design model for network data communication through UDP (this model is applicable to server,

It is also applicable to clients. But there are minor differences in implementation ):

 

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.