Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does Socket's mind mean?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What does Socket refer to? in view of this question, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Java is very convenient and powerful to develop network software. This power of Java comes from his unique set of powerful api for the network. These API are a series of classes and interfaces, all in the package java.NET and javax.net. In this article, we will introduce the concept of Socket and give examples to illustrate how to use Network API to manipulate sockets. After completing this article, you can write low-end network communication software.

What is a socket (Socket)?

Network API is typically used to communicate with other programs based on TCP/IP network Java programs, and Network API relies on Socket for communication. Socket can be regarded as an endpoint in the communication connection between two programs. One program writes a piece of information into Socket, and the Socket sends this information to another Socket, so that this information can be transmitted to other programs. As shown in the figure 1XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" / >

ASPectratio= "t" vgroup extt = "edit" >

20030107/jt-2003-image001.gif "src=" / Develop/ArticleImages/24/24004/CSDN_Dev_Image_2004-2-41116030.gif ">

Let's analyze the program An in figure 1, which writes a piece of information into Socket. The content of Socket is accessed by the network management software of Host A, and the information is sent through the network interface card of Host A to the network interface card of Host B Host B. after receiving this information, it is transmitted to the network management software of Host B. the network management software stores this information in the Socket of Host B. Program B can then read this message in Socket.

Assuming that a third host, Host C, is added to the network in figure 1, how does Host A know that the information is correctly transferred to Host B instead of to Host C? Based on the fact that each host in the TCP/IP network is assigned a unique IP address, the IP address is a 32-bit unsigned integer, which is usually separated by a decimal point, such as 198.163.227.6, because it is not converted to binary. As you can see, the IP address consists of four parts, each with a range of 0-255to represent an 8-bit address.

It is worth noting that IP addresses are 32-bit addresses, which is stipulated in IP Protocol version 4 (ipv4). At present, because IPv4 addresses are nearly exhausted, ipv6 addresses are gradually replacing Ipv4 addresses, while Ipv6 addresses are 128-bit unsigned integers.

Assuming that the second program is added to Host B of the network in figure 1, how can the information from Host A be correctly passed to program B rather than to the newly added program? This is because every program based on TCP/IP network communication is assigned a unique port and port number, which is an information buffer used to retain input / output information in Socket. The port number is a 16-bit unsigned integer in the range of 0-65535 to distinguish each program on the host (the port number is like the room number in a house), and short numbers less than 256are reserved for standard applications. For example, the port number of pop3 is 110. each socket is combined with the IP address, port and port number, so that the whole can distinguish each socket t. Let's talk about two kinds of sockets: stream sockets and self-addressable data sockets.

Stream socket (Stream Socket)

Whenever you send and receive information between two network applications, you need to establish a reliable connection. Stream sockets rely on TCP protocol to ensure that the information reaches the destination correctly. In fact, IP packets may be lost in the network or errors occur in the process of transmission. In any case, TCP, as the receiver, will contact the sender TCP to resend the IP packet. This is called establishing a reliable connection between two stream sockets.

Stream sockets play a necessary role in the Cramp S program, and the client program (a network application that needs to access certain services) creates a stream socket object that acts as the IP address of the host of the server program and the port number of the server program (the network application that serves the client application).

The initialization code of the client stream socket passes the IP address and port number to the network management software of the client host, and the management software passes the IP address and port number to the server host through NIC. The server host reads the data passed through NIC and then checks whether the server program is in a listening state, which is still done through sockets and ports. If the server program is listening, the server-side network management software sends a positive response signal to the client network management software. After receiving the response signal, the client stream socket initialization code establishes a port number for the client program. And pass this port number to the socket of the server program (the server program will use this port number to identify whether the information belongs to the client program) and complete the initialization of the stream socket.

If the server program is not in a listening state, the server-side network management software will send a negative signal to the client. After receiving this negative signal, the client program's stream socket initialization code throws an exception object and does not establish a communication connection, nor create a stream socket object. This situation is like making a phone call, when the communication is established when someone is there, otherwise the phone will hang up.

This part of the work includes three associated classes: InetAddress, Socket, and ServerSocket. The .netAddress object depicts a 32-bit or 128-bit IP address, the Socket object represents the client flow socket, and the ServerSocket represents the service program flow socket, all three classes are located in the package java.net

The answer to the question about what Socket refers to is shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report