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

How to analyze socket programming in Linux system

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

Share

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

This article analyzes "how to analyze socket programming in Linux systems". The content is detailed and easy to understand. Friends who are interested in "how to analyze socket programming in the Linux system" can follow the editor's idea to read it slowly and deeply. I hope it will be helpful to everyone after reading. Let's follow the editor to learn more about "how to analyze socket programming in Linux systems".

Introduction to socket: socket is the internal endpoint used to send or receive data within a node in a computer network. Specifically, it is a representation of this endpoint in network software (protocol stack), including communication protocol, target address, state, etc., and is a form of system resources.

Its position in the network is roughly the black part below, between the application layer and the transport layer.

The transport layer is where TCP/IP is located, and most of the applications you write through code belong to the application layer. Socket plays the role of connecting the application layer and the transport layer.

The birth of socket is to make it more convenient for applications to transmit data through the transport layer, so it essentially encapsulates the use of TCP/IP, and then the application can directly call socket API to communicate. So how does it work? It is divided into two parts, the server needs to establish a socket to listen to the specified address, and then wait for the client to connect. On the other hand, the client needs to establish socket and connect with the socket address of the server.

A simple example of the server side: after learning from the client side above, we already know how to create a socket, so the next step is to bind it to a specific port.

Bind socket to a port

The bind () function binds the socket to a port through which the client can initiate a request, and the socket corresponding to the port connects with the socket on the client side.

# include # Include int main () {int socket_desc; struct sockaddr_in server; socket_desc = socket (AF_INET,SOCK_STREAM,0); if (- 1==socket_desc) {perror ("socket create error\ n"); exit (1);} / / listening on the server itself server.sin_family=AF_INET; server.sin_port = htons (8888); server.sin_addr.s_addr = INADDR_ANY / / bind to port if (bind (socket_desc, (struct sockaddr*) & server,sizeof (server)) for socket binding to a specific port, the next thing we need to do is to accept all the data under that port. From the above implementation, we can see that a port can only be used by one socket. Listening port: after binding the socket to the port, we also need to listen to the port. To do this, we need to set the socket to be monitored. Listen () will be used to set socket to listening mode. Listen (socket_desc, 3); listen (int sockfd,int backlog); you can connect socket to receive requests while listening: # include # include int main () {int sock_desc,new_socket,sockaddr_size; struct sockaddr_in server,client; / / create socket sock_desc = socket (AF_INET,SOCK_STREAM,0); if (- 1==sock_desc) {perror ("cannot create socket\ n"); exit (1) } server.sin_family = AF_INET; server.sin_port = htons (8888); server.sin_addr.s_addr = INADDR_ANY; / / bind if (bind (sock_desc, (struct sockaddr*) & server,sizeof (server)) run the above code: output: waiting for incoming connecions. Now the code is running normally and waiting for a connection to be requested. In another terminal, we initiate a request: teltnet 127.0.0.1 8888 will output: trying 127.0.0.1connected to loaclhost in the current terminal. Escape character is;; connection closed by foreign host at the same time in the previous terminal, server will output: waiting for incoming connecions.connection accepted can see that server has correctly received the connection request from client and established a connection, but there is no subsequent action, and the host immediately closes the link. After the connection is established, the communication between the two parties can be carried out smoothly, and this part of the send is exactly the same as the recv operation. In addition, the server gets the ip address of the client: you can know that accept () returns the structure sockaddr_in, so it's easy to know the ip and port information of client. * char * client_ip = inet_ntoa (client.sin_addr); ``int`` client_port = ntohs (client.sin_port). The above is the Linux system-related content shared by Liangxu tutorials. If you want to know more about Linux, remember to follow the official account "good Linux", or scan the QR code below to follow, more practical information is waiting for you! On how to analyze the Linux system in the socket programming to share here, I hope that the above content can make you improve. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!

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