[Java network programming] Socket socket

Hello, everyone~ I am your old friend: Protecting Xiao Zhou ღ , this issue brings you the premise concept of network programming Socket socket, the operating system provides Socket to encapsulate the underlying protocol details and communication logic , enabling applications to interact with the network through a simple and intuitive API. So from an objective point of view, development based on Socket is network programming. Concept articles~~
Stay tuned for more excitement: Protecting Xiaozhou ღ *★,°*:.☆( ̄▽ ̄)/$:*.°★*'

1. Basic concepts of network programming

Network programming refers to writing programs (processes) that transmit data over a network. This includes developing applications that utilize network protocols, such as TCP/IP and HTTP, to enable communication and data exchange between different computers.

Each of our application layer programs can be regarded as a process, and each process will create a thread to run by default, which can be understood as the main() function in C/C++ or Java, and the main() function can be considered as the main thread , the main execution flow.

The greatest significance of the Internet lies in resource sharing. The significance of network programming is that it can make data exchange and communication between different computers possible, and connect computers and devices distributed in different locations together so that they can perform data transmission and sharing. resources, etc. As long as it is a program that transmits data based on the network, it belongs to the category of network programming.

In daily life, our networking is actually connected to the servers of major operators (China Mobile, China Unicom, etc.), our devices send requests to Baidu servers through Baidu browser, such as searching: pictures of beautiful women, Baidu servers (application terminal) will analyze this request to see if you have a picture of a beautiful woman. If so, the Baidu server will respond to the requesting device with the picture of a beautiful woman. These operations involve some knowledge points - the application of Baidu browser needs to be able to send information in the network, to whom: Baidu server, the ability to connect with other devices, receiving ability: Baidu browser and Baidu server After the connection is established, it is necessary to be able to truly and effectively receive and analyze the information sent by the Baidu server, so the Baidu server also needs to have the same function.

Process A: Obtain desired network resources through programming

Process B: Provide network resources needed by others through programming


1.1 Sending end and receiving end

During a network data transfer:

Sending end: refers to the device or program responsible for sending data in communication. The sending end converts the data to be transmitted into a specific signal or data packet, and sends it to the receiving end through the network or transmission medium. In computer network communication, the sender can be any device capable of sending messages, such as computers, mobile phones, servers, etc.

Receiver : Usually refers to a device or system that receives data during data transmission. In the field of communication, the receiving end can be a computer, mobile phone, TV, router, etc., which is used to receive information from the sending end and decode it for display, storage or further processing.

Transceiver: refers to a device or component capable of sending and receiving data. In the communication field, the transceiver end is often used as a general term for both ends of a communication link. The transceiver is a vital part of the communication system. They are responsible for transferring information from one node to another, and are the basis for realizing communication.

The sending end and the receiving end are just the concept of data flow generated by a network data transmission.


1.2 Request and Response

To give an example in life, Zhang San makes a call to Li Si. The first thing to do is to find Li Si's phone number, and then click to dial. : Moses Moses? , Li Si's operation can be regarded as responding to Zhang San's request. At this time, the sending end becomes Li Si's old iron, and the receiving end is Zhang San's old iron.

It’s also like going to a restaurant to have a meal, you say to the boss, boss, I want to eat fried rice with eggs-request, the boss responds, saying that he has received your request, and then quickly copies the meal, and before giving the meal to you, first Say hello, guest officer, your meal is ready: Serve an egg fried rice - Response.


1.3 Client and server

As mentioned above, the process of surfing the Internet in our daily life is to access the servers of other major operators. First, access the network in a large environment (China Mobile, China Unicom), and then link to the servers of each operator to access the corresponding servers. H.

Client: The process on the side trying to get the service is called the client. For example: use Baidu browser to access Baidu's server, search for pictures of beauties, the server says it's okay, I have a lot of them here, and I'll give them all to you, and then the application Baidu browser will get a lot of pictures of beauties.

Server: In the scenario of network data transmission, the process of the party that provides the service is called the server.

For the server, on the one hand, it provides the service resources that the client wants to obtain, and on the other hand, it provides the client with the function of saving resources on the server.


Two, Socket socket

Socket (socket) is a concept in network programming. It is a way to communicate through the network. It is a technology for network communication provided by the operating system. Socket is used to encapsulate the underlying protocol details and communication logic. Applications can interact with the network through a simple and intuitive API. So from an objective point of view, development based on Socket is network programming.

The real network adopts the TCP/IP network model. The lower-layer protocol provides services to the upper-layer protocol. The upper-layer protocol can directly call the lower-layer protocol, but cross-layer calls are not allowed. The transport layer provides services for the application layer. Therefore, Socket can also be regarded as It is the interface provided by the transport layer to the application layer.

The full name of API (Aplication Programming Interface), generally speaking, is a bridge for information interaction between software. For example, qq provides an interface (API) for account verification, and WeChat provides an interface for WeChat payment—for other applications to use, so some applications will It provides the functions of using QQ login, WeChat login, WeChat payment, etc. Of course, others need to pay Tencent for using this function.

2.1 Socket function classification

First of all, in a real network environment, the communication parties need to abide by the standard network protocol. The network protocol stipulates the communication specification, and the protocols are layered according to their functions. The common one is the TCP/IP five-layer network model:

The bloggers will not introduce the main functions of each layer protocol in detail. Interested friends can check another blogger’s blog: Protocols in the eyes of programmers: TCP/IP five-layer network model_protect Xiao Zhouღ’s blog-CSDN blog

Layered management of protocols, it is stipulated that lower-layer protocols provide services to upper-layer protocols, and upper-layer protocols call lower-layer protocols, and cross-layer calls are not allowed.

Network programming is developed from the perspective of the application layer, so the transport layer protocol needs to provide services for the application layer, and the application layer can only interact directly with the transport layer.

Socket (socket) is mainly divided into the following three categories for transport layer protocols:

  • Oriented byte stream socket: based on the transport layer TCP protocol

The byte stream-oriented socket is a network transmission method, which is implemented based on the transport layer TCP protocol. It treats data as an unstructured byte stream and guarantees data reliability during data transmission.

Features of the TCP protocol :

  1. Connection-oriented: before transmitting data, the two communicating parties first establish a reliable connection
  2. Reliable transmission: The TCP protocol has a timeout retransmission mechanism and an acknowledgment response mechanism. When it is found that there is no problem with the connection, but the data is not successfully transmitted, the data will be resent.
  3. Byte stream oriented: literally, use byte stream transmission
  4. Receive buffer, send buffer: In the network data communication, the buffer used to store the data to be sent or received, with the concept of the buffer, can help coordinate the data transmission speed between the sender and the receiver, And minimize the risk of data loss or confusion...
  5. Unlimited size of transferred data: byte stream, streaming, provided there is a connection.
  6. Full-duplex communication: Both communication parties can exchange information at the same time
  • Datagram-oriented socket: based on the transport layer UDP protocol

Datagram-oriented socket is a network communication method based on the transport layer UDP protocol. It does not provide reliability guarantees and flow control mechanisms, but has low latency and is suitable for real-time applications.

Features of the UDP protocol :

  1. Connectionless: before transmitting data, the communicating parties do not depend on establishing a connection, but only need to know who is sending it to whom.
  2. Unreliable transmission: only responsible for sending, not paying attention to whether the data transmission is successful, and doing nothing if it is not successful.
  3. Datagram-oriented: UDP datagrams are used for transmission, and datagrams can be understood as data being transmitted piece by piece.
  4. There is a concept of cache
  5. The size of data transmission is limited: UDP datagram can occupy a maximum of 64k, 1k = 1024 bytes
  6. Full-duplex communication: Both communication parties can exchange information at the same time

 For specific information, you can watch the blog post of the blogger UDP protocol

  • Oriented to raw sockets: based on a custom transport layer protocol

 Raw socket: It is a socket that processes raw network packets. Its main purposes are:

  • Send custom IP packets
  • Send ICMP packets
  •  The listening mode of the network card, listening to the data packets on the network
  • Masquerade IP address
  • Implementation of the self-positioning protocol

So far, bloggers have finished sharing the concept of Socket sockets in network programming. I hope it will be helpful to everyone. If there is anything wrong, please criticize and correct. 

This issue is included in the blogger's column - JavaEE, which is suitable for programming beginners. Interested friends can subscribe to view other "JavaEE basics".

Next issue preview: Java uses Socket sockets to implement client-server communication based on the UDP protocol.

Thank you to everyone who read this article, and more exciting events are coming: Protect Xiaozhou ღ *★,°*:.☆( ̄▽ ̄)/$:*.°★* 

Met you, all the stars are falling on my head...

Guess you like

Origin blog.csdn.net/weixin_67603503/article/details/130204097