Java programming 9-network programming Basics

Source: Internet
Author: User

Java programming 9-network programming Basics

Author: Chen yuefeng

From: http://blog.csdn.net/mailbomb

1.4 network programming Basics

For beginners, or programmers who have never been familiar with network programming, they will feel that the knowledge involved in network programming is very advanced and difficult. In fact, this is a misunderstanding. When you are familiar with the syntax, in fact, basic network programming has now been implemented abnormally simple.

1.4.1 what is network programming?

Network Programming is essentially a data exchange between two devices. Of course, in a computer network, a device mainly refers to a computer. Data transmission is not very difficult. It is not to send data from one device to another device, but to receive data from another device.

The current network programming is basically based on the request/response method, that is, a device sends request data to another device and then receives feedback from another device.

In network programming, the program that initiates the Connection Program, that is, the program that sends the first request, is called the client, and the program that waits for other programs to connect is called the server ). The client program can be started as needed, and the server needs to be started all the time in order to be able to connect at the same time. For example, if you use a phone call as an example, the person who calls the phone is similar to the client, and the person who receives the phone must keep the phone open and similar to the server.

After the connection is established, the client and server can transmit data, and the identities of the two are equivalent.

In some programs, the program has both client and server functions. The most common software is Bt and eMule.

Next we will talk about how to establish a connection and how to send data.

1.4.2 IP address and domain name

In real life, if you want to make a phone call, you need to know the phone number of the corresponding person. If you want to send a mail, you need to know the recipient's address. This is also true in the network. If you need to know the location of a device, you need to use the IP address of the device. The specific connection process is implemented by hardware, so programmers do not need to worry too much.

An IP address is a rule that uses IPv4, which consists of four numbers ranging from 0 to 25. It only takes four bytes to store the IP address in the computer. In a computer, the IP address is assigned to the network card. Each network card has a unique IP address. If a computer has multiple network cards, the computer has multiple different IP addresses, the IP addresses in the same network cannot be the same. The concept of IP address is similar to that of phone number and ID card.

Because the IP address is not easy to remember, the concept of domain name is specifically created, in fact, it is to give the IP address a character name, such as 163.com, Sina.com and so on. There is a relationship between the IP address and the domain name. If you compare an IP address to an ID card number, the domain name is your name.

In fact, only IP addresses can be used for data transmission in the network. Therefore, before transmission, you need to convert the domain name to an IP address. This is done by a server called DNS.

In network programming, IP addresses or domain names can be used to identify a device on the network.

1.4.3 port Concept

In order to run multiple programs on a device, the concept of port is designed manually. A similar example is the internal extension number of the company.

It is required that a device has 216 or 65536 ports. Each port corresponds to a unique program. Each network program, whether a client or a server, corresponds to one or more specific port numbers. Since the period between 0 and 1024 is mostly occupied by the operating system, the port number after is generally used in actual programming.

You can find a unique program on a device by using the port number.

Therefore, to establish a connection with a computer, you only need to know the IP address or domain name, but if you want to exchange data with a program on the computer, you must also know the port number used by the program.

1.4.4 Data Transmission Mode

Now that you know how to establish a connection, the following describes how to transmit data. Let's take a look at the data transmission method.

On the network, there are two data transmission methods, whether wired transmission or wireless transmission:

L TCP (Transfer Control Protocol)

Transmission Control Protocol, which is a stable and reliable transmission method, similar to the call in the display. You only need to establish a connection to transmit data multiple times. Just like dialing a phone number once, you can keep on talking. If you are not clear about the phone number, the other party will ask you to repeat the phone number to ensure reliable data transmission.

The advantage of using this method is stability and reliability. The disadvantage is that the cost of establishing a connection and maintaining the connection is high, and the transmission speed is not fast.

L UDP)

User Datagram Protocol, which does not establish a stable connection, similar to sending short messages. Data is directly sent each time it is sent. To send multiple text messages, you must enter the recipient's number multiple times. This transmission method is not reliable, and data may not be received. The system only tries its best to send the data.

This method has the advantage of low overhead and fast transmission speed. The disadvantage is that data may be lost.

In actual network programming, you can select any transmission mode as needed, or use these two methods in combination to transmit data.

1.4.5 protocol Concept

Protocol is a very important concept in network programming. It refers to the format of data transmission. Because we need to transmit all kinds of information in the network and get a set of values in the program, we need to specify the format of this set of data in advance to read these values, the sending data is generated on the client according to this format. The server reads the data according to this format, and then generates the data to the client according to a certain format. The client then reads the data in this format. In reality, similar examples are telegraph code. Each number is expressed with specific data.

Generally, the protocol of a program is divided into the data format sent by the client and the data format fed back by the server. both the client and the server follow this format to generate or process data, implement complex data exchange between the two.

1.4.6 Summary

Network Programming is to use an IP address or domain name to connect to the corresponding program on another computer and exchange data according to the specified protocol (Data Format, in actual programming, establishing connections and sending and receiving data has been achieved at the language level. More work is to design protocols and compile code for generating and parsing data, then, convert the data into a logical structure to display or control the logic.

To learn more about network programming, read the book Java2 network protocol insider.

 

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.