Home  >  Article  >  What is socket programming

What is socket programming

小老鼠
小老鼠Original
2023-07-07 10:55:141832browse

Socket programming is the programming interface provided by the transport layer to the application layer. It is a bridge between the application layer and the transport layer. Using Socket programming, you can develop client and server applications, communicate on the local network, and also Can communicate globally via the Internet.

What is socket programming

1. Introduction to socket programming

The so-called socket is usually also called "socket", which is used to describe IP addresses and ports. A handle to the communication chain. Applications usually make requests to the network or respond to network requests through "sockets". The network applications we develop are located at the application layer. TCP and UDP are transport layer protocols. How to use the transport layer services at the application layer? Between the application layer and the transport layer, sockets are used for separation.

The socket is like a small opening opened by the transport layer for the application layer. The application sends data to the remote through this small opening, or receives data from the remote; and within this small opening, that is, the data enters this After the port, or before the data comes out of this port, we don't know or need to know, and we don't care about how it is transmitted. This is the work of other levels of the network.

Socket is actually a programming interface provided by the transport layer to the application layer. The transport layer provides a logical channel from process to process based on the network layer, while the application layer process uses the transport layer to communicate with a certain process on another host. Socket is the bridge between the application layer and the transport layer. Using Socket programming, you can develop client and server applications, communicate on the local network, and communicate globally through the Internet.

2. What happens when two computers use sockets to establish a TCP connection

The server instantiates a ServerSocket object, indicating communication through the port on the server. The server calls the accept() method of the ServerSocket class, which waits until a client connects to the given port on the server. While the server is waiting, a client instantiates a Socket object, specifying the server name and port number to request a connection. The constructor of the Socket class attempts to connect the client to the specified server and port number. If communication is established, a Socket object is created on the client to communicate with the server. On the server side, the accept() method returns a new socket reference on the server that is connected to the client's socket.

The above is the detailed content of What is socket programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn