If you try to write to a socket that is no longer connected, instead of providing a helpful error, the system happily generates a SIGPIPE signal, as you've found. If you don't handle SIGPIPE, your process ends. To ensure this doesn't happen, you can disable SIGPIPE for the socket: int val = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof(val)) != 0) // something went wrong - this is unlikely to happen -josh On Feb 19, 2013, at 10:40 AM, Arjun SM <arjun.sm@gmail.com> wrote:
Hi all,
I have implemented a daemon which tries to make a connection to a server using non-blocking sockets and works fine. But the problem surfaces, when I put my Mac machine to sleep for few hours and wake it up. My daemon hangs and i get a SIGPIPE on the error console after some time. I am running Mountain Lion OS X 10.8.2 version.
The MakeConnect() function which I am using in my daemon is fairly simple and below is an excerpt, m_AgencySock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
set the socket to non-Blocking int err = fcntl(m_AgencySock, F_SETFL, O_NONBLOCK);
int status = connect(m_AgencySock, (struct sockaddr *)&m_AgencyAddress, sizeof(m_AgencyAddress)); if (status < 0) { status = errno; // Check the error number to get our current status if (status == EINPROGRESS) { FD_ZERO(&readFDs); FD_ZERO(&writeFDs); FD_SET(m_AgencySock, &readFDs); FD_SET(m_AgencySock, &writeFDs);
Call select with struct timeval waitd in a while loop and check for selectResult > 0 and break; waitd.tv_sec = 10; waitd.tv_usec = 0; selectResult = select(m_AgencySock + 1, &readFDs, &writeFDs, NULL, &waitd);
} }
I am resetting the struct timeval waitd before calling select() again.
is there any other parameters that needs to addressed?
thanks in advance, ~Arjun
_______________________________________________ Do not post admin requests to the list. They will be ignored. Macnetworkprog mailing list (Macnetworkprog@lists.apple.com) Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/macnetworkprog/jgraessley%40apple.co...
This email sent to jgraessley@apple.com
_______________________________________________ Do not post admin requests to the list. They will be ignored. Macnetworkprog mailing list (Macnetworkprog@lists.apple.com) Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/macnetworkprog/site_archiver%40lists... This email sent to site_archiver@lists.apple.com