Non-blocking mode, although connect error, but GetSockOpt made error is 0 of the problem __ Network

Source: Internet
Author: User

When you debug your project code, you find a strange problem that is recorded as follows:

In non-blocking mode, connect initiates a chain and returns-1 (this is a normal phenomenon in non-blocking mode). Then listen to the write event for the socket, and when the getsockopt function gets the error (SO_ERROR) when the write event is triggered, no error is detected (the fourth parameter returns 0), and the last error occurs in the write operation.

Reason: Due to a problem with the profile, the resulting IP address to the end is an empty string "":

	struct sockaddr_in saddr;
	SADDR.SIN_ADDR.S_ADDR = inet_addr ("");
	saddr.sin_family = af_inet; /* "Default" Family * *
	Saddr.sin_port = htons (22);

However, when Connect returns-1, the error code is not detected as einprogress:

		if (Connect (M_hsocket, paddr, naddrlen) = = 0)
		{return
			true;
		}
		else
		{return
			false;
		}
Then you start listening for write events directly. The write event is triggered immediately because the write connection on the socket socket has been turned off because of a connect error.

When getsockopt is invoked, the error code obtained is 0, and no error is considered.

In the next write operation, write writes to a closed connection, causing the sigpipe signal to be triggered.


Summarize:

Although the non-blocking mode of connect, generally are returned to 1, but forgot to detect whether errno is einprogress, like this problem, because the address is wrong, connect return-1, but at this time the error is "Network is Unreachable ", in which case you can no longer listen for write events on the socket and call getsockopt in the callback function because getsockopt cannot get the error code and will only return 0.

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.