SIGPIPE signal in socket programming and sigpipe in socket programming

Source: Internet
Author: User
Tags java throws

SIGPIPE signal in socket programming and sigpipe in socket programming

I wrote a server programLinuxAnd then write the client in C ++ to perform stress testing with tens of millions of short links.However, the Server Always exits without core files.

Finally, the problem is determined to call two write operations on a socket that has been closed on the peer end. The second write operation will generateSIGPIPESignal, which ends the process by default.

The specific analysis can be combined with TCP's "Four handshakes" to close. TCP is a full-duplex channel. It can be seen as two ticket channels. The two ends of the TCP connection are each responsible for one. when the peer calls close, although the intention is to close the entire two channels, the local end only receives the FIN package. according to the semantics of the TCP protocol, the peer only closes the ticket channel in charge of it and can continue to receive data. that is to say, due to the restrictions of the TCP protocol, an endpoint cannot know whether the socket of the Peer side calls close or shutdown.

Call the read method for a socket that has received the FIN packet. If the receiving buffer is empty, 0 is returned. This is often said to indicate that the connection is closed. but when you call the write Method for it for the first time, if there is no problem with the sending buffer, the correct write (sending) will be returned ). however, the sent packets will cause the Peer to send the RST packet, because the socket at the peer end has already called close and is completely closed, neither sending nor receiving data. therefore, the second call to the write method (assuming that after receiving the RST) will generateSIGPIPESignal, causing the process to exit.

To avoid process exit, captureSIGPIPESignal, or ignore it and set itSIG_IGNSignal processing functions:

Signal(SIGPIPE,SIG_IGN);

In this way,-1 is returned when the second write method is called, and errno is setSIGPIPEThe program will be able to know that the Peer has been closed.


InLinuxWhen writing a socket program, if you try to send it to a disconnected socket, the underlying layer will throwSIGPIPESignal.
The default processing method for this signal is to exit the process, which is not what we expect most of the time. Therefore, we need to reload the processing method of this signal. Call the following code to secure blocking.SIGPIPE:

Signal (SIGPIPE, SIG_IGN );

The reason why my program generates this signal is:
After the client sends a message to the server through pipe, it closes the client. At this time, the server generates a Broken pipe signal when it returns the message to the client, the server will be ended by the system.


We can use this method before generating a signal.Signal(Int signum, sighandler_t handler) set signal processing. If this method is not called, the system will call the default processing method: Abort the program and display a prompt (that is, we often encounter problems ). You can call the system's processing methods or customize the processing methods.

Three processing methods are defined in the system:
(1) default actions for SIG_DFL signals:
(A) if the default action is to suspend a thread, the thread's execution is temporarily suspended. When the thread is paused, no additional signals sent to the thread are delivered until the thread starts executing, except for SIGKILL.
(B) Set the signal action of the suspended signal to SIG_DFL, and the default action is to ignore the signal (SIGCHLD ).
(2) SIG_IGN ignores Signals
(A) The delivery of the signal has no effect on the thread.
(B) The system does not allow the operation of the SIGKILL or SIGTOP signal to be set to SIG_DFL.
3) SIG_ERR

Project I calledSignal(SIGPIPE,SIG_IGN). SIGPIPEWhen the signal is sent, the program will not be aborted, and the signal will be ignored directly.


My socket server often ends the process due to the SIGPIPE signal. I don't know how to set the SIGPIPE signal to be ignored in java.

I tried to help you find out that the sigpipe signal occurs because one side closes the channel and the other side is still writing. Generally, JAVA throws an exception at this time. You only need to catch it.
// Outer loop while (true) {// do something // read socket data try {while (next = in. read (buf ))! =-1) {}} catch (IOException e) {// catch exception. In this case, the outer loop is not affected. throw new GroovyRuntimeException ("excep while dumping process stream", e );}} reference: stackoverflow.com/..ceived

What is Socket in Socket programming?

In short, it is a communication convention between the two parties. related functions in the socket are used to complete the communication process.

When the application layer uses the transport layer for data communication, TCP and UDP may encounter concurrent services for multiple application processes at the same time. Multiple TCP connections or multiple application processes may need to transmit data through the same TCP port. To differentiate different application processes and connections, many computer operating systems provide interfaces called Sockets for applications to interact with TCP/IP protocols.

Distinguish network communication and connection between processes of different applications. There are three main parameters: the destination IP address for communication, the transport layer protocol (TCP or UDP) used, and the port number used. The original intention of Socket is "Socket ". By combining these three parameters and binding them with a "Socket" Socket, the application layer can distinguish communications from processes or network connections of different applications through the Socket interface with the transport layer, implements concurrent data transmission services.

-- Win API socket
If the Socket function mentioned in this article is not specifically described, it refers to the Windows Socket API.

1. WSAStartup Function
Int WSAStartup (
WORD wVersionRequested,
LPWSADATA lpWSAData
);
The program using the Socket must call the WSAStartup function before using the Socket. The first parameter of this function indicates the Socket version requested by the program. The high byte indicates the secondary version and the low byte indicates the primary version; the operating system uses the second parameter to return the request's Socket version. When an application calls the WSAStartup function, the operating system searches for the corresponding Socket Library Based on the requested Socket version, and then binds the Socket Library to the application. In the future, the application can call other Socket functions in the requested Socket library. After the function is successfully executed, 0 is returned.
For example, if a program uses a Socket of Version 2.1, the program code is as follows:
WVersionRequested = MAKEWORD (2, 1 );
Err = WSAStartup (wVersionRequested, & wsaData );

Ii. WSACleanup Function
Int WSACleanup (void );
After the application completes the use of the requested Socket library, it needs to call the WSACleanup function to unbind from the Socket library and release the system resources occupied by the Socket library.

Iii. socket Functions
SOCKET socket (
Int af,
Int type,
Int protocol
);
The application calls the socket function to create a socket capable of network communication. The first parameter specifies the protocol family used by the application. For the TCP/IP protocol family, this parameter is set to PF_INET. The second parameter specifies the socket type to be created, the stream socket type is SOCK_STREAM and the datagram socket type is SOCK_DGRAM. The third parameter specifies the communication protocol used by the application. This function returns the descriptor of the newly created socket if the call is successful, and INVALID_SOCKET if the call fails. The socket descriptor is an integer value. Each process has a socket Descriptor Table in the process space, which stores the correspondence between the socket Descriptor and the socket data structure. In this table, one field stores the descriptor of the newly created socket, and the other field stores the address of the socket data structure. Therefore, the corresponding socket data structure can be found based on the socket descriptor. Each process has a socket Descriptor Table in its own process space, but the socket data structure is in the kernel buffer of the operating system. The following is an example of creating a stream socket:
Struct protoent * ppe;
Ppe = getprotobyname ("tcp & qu ...... remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.