Location>code7788 >text

NIO Implementation of Chat Rooms: It all starts with a chat about the basics of network programming!

Popularity:855 ℃/2024-10-07 10:24:15

I. Writing at the beginning

Hello everyone, Brother Build is back! Stop more about 2 months long, before a period of time to write a novel to go, and then because the company live too much, cattle and horses dry too much commitment, pulled down the blog update, the National Day, a rare idle, ready to return to their old jobs.

Roughly looked over the content of the previous update, has been written to the IO part of Java, as a network transmission of a major focus of knowledge, IO is critical, and in order to better understand and use IO, we have to extend today to talk about network programming in Java. In fact, this sentence is not too strict, network programming is not unique to Java, all rely on the Web side of the programming language, or applications are inseparable from the network programming.

II. Foundations of network programming

OK, no more talk, let's get straight to the point, to learn about network programming, before we start learning, we should understand our daily life are inseparable from the Internet and peripheral products (routers, Web sites, cell phones, computers) and so on.

2.1 Computer networks

Computers are very familiar to us, before multiple networked computers, we can chat, video, e-mail exchanges, but also through different media platforms for interoperability, all the users online between the devices, like a large network connected, interoperability, which is the computer network!

image

As shown above, it is a generalized network topology sketch that encapsulates our current Internet world in a very visual and easy to understand way. Among them.user equipmentSuch as cell phones, computers these are used to access web resources such as watching dramas, brushing up on news, andnetwork switchOn the other hand, it is the bridge that allows the phone to access the Internet and is used to connect the devices on the LAN to the router, while therouter (computing)The main work is to realize the routing between different networks, which can be connected to the Internet, and in order to prevent the intrusion of some for authorized access to the device, the network of the device are equipped with afirewalls, which is used to intercept unauthorized requests; andthe Internetand, in turn, a network of information resources that are globally interoperable with each other.server (computer)Needless to say, processing and storage stations for hosting web applications and data.

So, see here we have such a question, different countries and regions of the world there are different LANs, following different network protocols, how to achieve global interoperability of the Internet? The great pioneers have long considered this point, therefore, the earliest development of the specification of the gang, the interconnection of the global protocol specification, that is, the TCP / IP protocol, the following when we talk about the protocol will say.

2.2 IP

For IP, we are very familiar with, we are in the daily process of surfing the Internet, said the URL, in fact, the main is this IP (Internet Protocol), the Chinese translation for the International Interconnection Protocol. It is like an identity card number, the only identification of a network interface, any computer connected to the Internet as long as the computer has an IP address.

at the present timeIPconsist ofIPv4cap (a poem)IPv6,on account ofv4version adopted32bit address,the more you count42Billions of addresses,at the present time已经用尽,That's why the launch of thev6releases,adoption128bit address,340万亿亿亿Billions of addresses,treat (sb a certain way),You read that right.,2(used form a nominal expression)128That's how many addresses there are.,这个数字是人类灭绝了都用不完(used form a nominal expression)。

A special IP address, called the local address, which is always 127.0.0.1

IPv4 (e.g. 101.302.88.22)
IPv6 (e.g. 2001:0DA8:100A:0000:0000:1020:F2F3:1428)

image

We can see the network information of our computer through ipconfig, our computer in addition to the local 127.0.0.1, there is an IP address, if more than one network card, there will be more than one IP address, then different computers before how to communicate?

Here's what's involved."network number"This concept, the network number is calculated by IP and subnet mask, if the network number between two computers is the same, it means that the two computers are in the same network and can communicate directly, if it is different, then it is not in the same network, and you need to communicate indirectly through a router or switch, which is a gateway.

2.3 Domain names

Above said that most of the addresses on the network refers to ip, many people will be strange, for example, we like to log in to Baidu, input is not the format of the, but, and the combination of this is thedomain nameWe use DNS to translate a domain name into a corresponding IP address! We use a domain name resolution server, DNS, to translate a domain name into a corresponding IP address, and the client accesses the server based on the IP address.

Obviously, a domain name is easier to remember than an IP address with a bunch of numbers in it! Let's take a look at thenslookupcommand to view the ip address corresponding to a domain name.

image

Of course, the local ip address we mentioned above127.0.0.1 There is also a corresponding local domain name:localhost

image

2.4 Network modeling

For a variety of devices common Internet this matter, not a few lines in our Figure 1 can handle da, its internal, underlying design is very complex, in order to be able to reach a common standard interface on a global scale, many organizations have issued a In order to reach a common standard interface on a global scale, many organizations have issued similar standards, the most famous and now generally recognized is -Open Systems Interconnection Communications Reference Model(English: Open System Interconnection Reference Model, abbreviation:OSI; referred to as the OSI model) is a conceptual model, proposed by the International Organization for Standardization, a standard framework that attempts to interconnect a variety of computers into networks around the world.

The OSI model is divided into seven layers, from bottom up Physical Layer, Data Link Layer, Network Layer, Transport Layer, Session Layer, Presentation Layer, Application Layer. Layer), Presentation Layer, and Application Layer.

image

But what we actually use in the Internet is the TCP/IP model, which does not correspond to OSI's 7-layer model, but roughly corresponds to OSI's 5-layer model, and some people say it's a 4-layer model, which depends on yourself, it doesn't matter, the main thing is to understand the process.

2.5 Common protocols

In the OSI model above, we can see in the application layer, transport layer, network layer, data link layer corresponds to a number of network protocols, we can collectively referred to as the bit TCP / IP protocol cluster, we will briefly introduce a few of the important protocols:

  1. IP (Internet Protocol): a protocol for packet switching that does not guarantee reliable transmission and is responsible for routing packets from a source host to a destination host, a protocol at the network layer;
  2. TCP protocol (Transmission Control Protocol): TCP protocol is built on the basis of IP, is the transport layer protocol. the IP protocol is responsible for the transmission of data, while the TCP protocol can be established on the basis of a secure connection, control the data transmission, to ensure the reliability, and support bidirectional communication, such as HTTP, HTTPS are built on top of the TCP protocol.
  3. UDP Protocol (Datagram Protocol): Like TCP protocol, it is a transport layer protocol, the difference is that it is a connectionless protocol and does not guarantee reliable transmission. There is no need to establish a connection before communication, so its transmission efficiency is higher than that of TCP, and the UDP protocol is much simpler than the TCP protocol. When the UDP protocol is chosen, the transmitted data is usually tolerant of loss, for example, the UDP protocol is chosen for some voice-video communication applications.

III. Summary

Today said so much, the main introduction to the basics of network programming, network programming in the subsequent blog post for the content required, in the further organization and refinement.