Java must learn and be able to network programming

1、 Basic concepts of network

First, clarify a concept: network programming= Website programming and network programming are now generally called TCP / IP programming.

2、 Network communication protocol and interface

3、 Layered idea of communication protocol

4、 Reference model

5、 IP protocol

Everyone's computer has a unique IP address so that they can communicate with each other without sending wrong information.

The IP address is divided into four segments by a point. In the computer, the IP address is represented by four bytes. One byte represents a segment, and the maximum number represented by each byte can only reach 255.

6、 TCP protocol and UDP protocol

TCP and UDP are located on the same layer and are based on the IP layer. Because the two computers have different IP addresses, the two computers can be distinguished and can talk to each other. There are generally two call modes: the first is TCP and the second is UDP. TCP is a reliable connection. TCP is like making a phone call. You need to call the other party first and wait for the other party to respond before continuing to talk to the other party. That is, you must confirm that you can send a message before sending the message. Everything uploaded by TCP is reliable. As long as a connection is established between the two machines, the data sent on the machine will be transmitted to the other machine. UDP is like a telegram. It is finished when it is sent. It doesn't matter whether the other party receives it or not, so UDP is unreliable. Although TCP transmits data reliably, it transmits slowly. UDP transmits data unreliably, but it transmits fast.

7、 Socket programming

General network programming is called socket programming. Socket means "socket" in English.

Install a socket on both computers, and then plug both ends of a cable into the socket of the two computers, so that the two computers can establish a good connection. This socket is socket.

Because they can communicate with each other, I say you are my server, but in a logical sense, I should send things to you first, and then you can handle and forward them. So your name is server. But technically speaking, only TCP can be divided into server and client. For UDP, strictly speaking, there are no so-called server and client. The socket of TCP server is called ServerSocket, and the socket of client is called socket.

If two computers are connected to each other, you must first know their IP addresses, but it is not enough to provide only IP addresses. You must also have a connection port number, that is, which application you want to connect to.

Port numbers are used to distinguish between different applications on a machine. The port number occupies 2 bytes inside the computer. There are a maximum of 65536 port numbers on a machine. An application can occupy multiple port numbers. If the port number is occupied by one application, other applications can no longer use the port number. Remember, if the program we write needs to occupy the port number, use the port number above 1024, and do not occupy the port number below 1024, because the system may be requisitioned at any time. The port number itself is divided into TCP port and UDP port. The 8888 port of TCP and the 8888 port of UDP are two completely different ports. There are 65536 TCP and UDP ports.

8、 TCP socket communication model

9、 Socket usage example

Server side ServerSocket

Client socket

The client requests a connection from the server through port 6666. After the server accepts the connection request from the client, it installs a socket on the server, and then connects the socket with the client's socket, so that the server can communicate with the client. When another client requests a connection, the server accepts it, Another socket will be installed to connect with the socket of this client.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>