In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the example analysis of Socket communication for everyone. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.
Two programs on a network exchange data over a two-way communication connection, one end of which is called a socket. At least one pair of sockets is required to establish a network communication connection. Socket is essentially a programming interface (API), encapsulation of TCP/IP, TCP/IP also provides an interface for programmers to do network development, this is the Socket programming interface;HTTP is a car, providing a specific form of encapsulation or display data;Socket is an engine, providing the ability to communicate on the network.
Socket communication is a kind of process communication, first list the types of process communication:1) pipe: pipe is a half-duplex communication mode, data can only flow in one direction, and can only be used between processes with kinship. The kinship of processes is usually referred to as the parent-child relationship. 2) FIFO: Named pipes are also half-duplex communication methods, but are allowed to be used between unrelated processes. Pipes are first-in, first-out communication methods. Semaphore: A semaphore is a counter that can be used to control access to shared resources by multiple processes. It is often used as a locking mechanism to prevent a shared resource from being accessed by another process while it is being accessed. Therefore, it is mainly used as synchronization hand pieces between processes and between different threads within the same process. 4) Message queue: Message queue is a linked list of messages stored in the kernel and identified by message queue identifier. Message queue overcomes the shortcomings of less signaling information, pipeline can only carry unformatted byte stream and limited buffer size. 5) Signal: A signal is a more complex form of communication used to inform the receiving process that an event has occurred. Shared memory maps a memory that can be accessed by other processes, created by one process but accessible by multiple processes. Shared memory is the fastest IPC method and is specifically designed for the inefficiency of other methods of interprocess communication. It is often used in conjunction with other communication mechanisms such as semaphores to achieve synchronization and communication between processes. 7) Socket: Socket is also an inter-process communication mechanism, different from other communication mechanisms, it can be used for process communication between different machines.
Socket can be used for process communication between different machines. In general, socket is used more. Common interfaces are socket, bind, listen, connect, accept, send, recv, close. The processing of specific functions is not described in detail here, only some details are described.
1: The order of function calls from the server is socket-> bind-> listen ->accept
First use the socket function to create a socket descriptor, after the creation can use setsockopt to set socket options, common options:SO_REUSEPORT Set this option can reuse the port number, for example, if the TCP connection is in the TIME_WAIT state, then the port can not be reused within 2MSL time So if there is a server trying to bind this port will fail, use this option to solve this problem.TCP_NODELAY turns off Nagle of TCP, Nagle algorithm is enabled by default.
2: Use the listen function to listen for connection requests on a socket. The prototype function is:
int listener (int sockfd ,int backlog) The second parameter backlog is used to specify the maximum number of outstanding connections to listen to the socket. For TCP INET, after receiving a connection request from the client, the server creates a new SOCKET for the connection request and puts the SOCKET in the queue for receiving the connection; after the connection is established, the newly created SOCKET is cleared from the receive queue. If the application layer does not have time to call accept() to receive connections under certain circumstances, and there are many clients initiating connections to the server at this time, it may cause the server-side receive connection queue to grow indefinitely. To prevent this situation, specify a limit on the length of the server-side SOCKET receive queue in the listen function. The default value is 128. When the setting is greater than 128, the actual value is 128. If the socket receive queue for receiving connections on the server side exceeds the value of backLog, the new connection request is rejected.
3: Message sending and receiving function
TCP uses send and recv: This is because each TCP has a connection, and after each connection is completed, the connection information will be recorded in the socket, so that each time the data is sent and received, the IP address and port number of the other party are known, and there is no need to specify again. Recording occurs after the accept and connect calls are completed. 1) The connect call is logged locally because there is only one socket connection. 2) For accept, because there are multiple connections, accept will return a socket object corresponding to a TCP connection, recording the corresponding IP and port.
UDP uses sendto() and recvfrom()1) recvfrom to return the address of the sender, so for the server, since the UDP socket object does not record the corresponding IP and port information, it will need to use the address to bring a response to the client. For the client, since the IP address and port of the server are always known each time (interacting with a server), there is no need to record (unless the UDP client needs to interact with multiple servers, it needs to be recorded one by one to ensure correct interaction) 2) sendto Since the IP and port are not recorded in the socket object, it is necessary to specify the recipient address each time, both the client and the server need to use
4:socket combined with epoll
In process communication, socket and epoll are often combined to handle connection requests and message sending. First, the server creates a socket descriptor, binds the specified port with bind, and initiates listening. Then, the socket descriptor is placed in epoll, so that the client's connection request can be notified to the socket server by epoll. If there is a client connection request, use the accept function to create a new descriptor. This new socket descriptor is used to listen for messages sent by the client. If there is a message to read, then you can use the recv function to read the message endlessly until the data is finished.
About "Socket communication example analysis" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.