Port and socket, used to determine which local process on the specified host uses which Protocol to communicate with which process on the remote host. The use of ports and sockets can be based on the following:
① Assign a process ID to each application process. Each time a process is started, the ID may be different.
② Process IDs vary with operating system platforms, so they are inconsistent.
③ A server process can be connected to multiple clients at the same time, so a simple connection identifier cannot be unique.
The concept of port and socket provides a uniform way to uniquely identify the connection and the program and host involved in the connection, regardless of the specific process ID.
(1) Port
The communication port that can be named and addressable in the network is a resource that can be allocated by the operating system.
According to the description of the OSI Layer-7 protocol, the main difference between the transport layer and the network layer is that the transport layer provides process communication capabilities. In this sense, the final address of network communication is not only the host address, but also an identifier that can describe the process. To this end, the TCP/IP protocol puts forward the concept of protocol port (port), which is used to identify the communication process.
A port is an abstract software structure (including some data structures and I/O buffers ). After an application (process) establishes a binding with a port through a system call, the data transmitted by the transport layer to the port is received by the corresponding process, the data sent by the corresponding process to the transport layer is output through this port. In the implementation of TCP/IP, port operations are similar to General I/O operations. A process obtains a port, which is equivalent to obtaining a local unique I/O file, it can be accessed using common read/write primitives.
Similar to file descriptors, each port has an integer identifier called port number, which is used to distinguish different ports. Because the TCP and UDP protocols of the TCP/IP transport layer are completely independent software modules, their respective port numbers are also independent of each other. For example, TCP has a port 255, UDP can also have port 255, which does not conflict with each other.
Port number allocation
Port number allocation is an important issue. There are two basic allocation modes: the first is global allocation, which is a centralized control mode. A recognized central organization uniformly allocates the results according to user needs and publishes them to the public. The second is local allocation, also known as dynamic connection. When a process needs to access the transport layer service, it applies to the local operating system. The operating system returns a unique local port number, the process then associates itself with the port number through appropriate system calls (bundled ). The above two methods are integrated in TCP/IP Port number allocation.
A well-known port is a well-known port number ranging from 0 to 1023. These ports are usually allocated to some services. For example, port 20/21 is allocated to the FTP (file transfer protocol) service, port 23 is allocated to telnet (Remote logon), and port 25 is allocated to the SMTP (Simple Mail Transfer Protocol) service, port 80 is allocated to the HTTP service, and port 135 is allocated to the RPC (Remote process call) service.
Network services can use other port numbers. If it is not the default port number, you should specify the port number in the address bar by adding the colon ":" (half-width) after the address ), add the port number. For example, if "8080" is used as the WWW Service port, you must enter ": 8080" in the address bar ".
However, some system protocols use a fixed port number, which cannot be changed. For example, port 139 is used for communication between NetBIOS and TCP/IP and cannot be changed manually.
However, dynamic ports are often used by viruses and Trojans. For example, the default connection ports of glaciers are 7626, way 2.4 is 8011, NetSpy 3.0 is 7306, and Yai is 1024.
(2) socket
A socket exists in a communication area, which is also called an address family. It is an abstract concept used to combine the common features of processes that communicate through a socket. Generally, a socket only exchanges data with a socket in the same region (cross-region communication is also possible, but this can only be achieved after a certain conversion process is executed ). Windows Sockets only supports one communication region: af_inet, which is used by processes that communicate with the Internet protocol family.
Category:
The following are three socket types of common TCP/IP protocols. Stream socket (sock_stream): A stream socket is used to provide connection-oriented and reliable data transmission services. This Service ensures that data can be sent without errors or duplicates and received in order (the data may change in order during transmission, but the receiving end finally receives the data in order ). The reason why stream sockets can implement reliable data services is that they use the Transmission Control Protocol (TCP.
Datagram socket (sock_dgram): A datagram socket provides a connectionless service. This service does not guarantee the reliability of data transmission. data may be lost or duplicated during transmission, and data may not be received sequentially. The datagram socket uses the user data protocol for data transmission. Because the datagram socket cannot guarantee the reliability of data transmission, the program must handle possible data loss.
Original socket (sock_raw): the difference between the original socket and standard socket (standard socket refers to the stream socket and datagram socket described earlier) is: the original socket can read and write IP data packets not processed by the kernel, while the stream socket can only read TCP data, and the datagram socket can only read UDP data. Therefore, if you want to access other protocols to send data, you must use the original socket.
Communication: There are two basic modes for data processing using sockets: synchronous and asynchronous. Synchronization mode: the synchronization mode is used to connect, receive, and send data through a socket. The client and server are blocked before receiving the response from the other party, that is, the following statement is executed until the other party receives the request. It can be seen that the synchronization mode is only applicable to scenarios with less data processing. When the program executes many tasks, a long wait may make the user unable to endure. Asynchronous mode: the asynchronous mode is used to connect, receive, and send data through a socket. The client or server is not blocked. Instead, the callback mechanism is used to connect, receive, and send data, in this way, you can directly return the sent or received method and continue to execute the following program. It can be seen that asynchronous sockets are especially suitable for massive data processing. It is relatively simple to program with synchronous sockets, while asynchronous socket programming is complicated.
Zookeeper