I use a TCP server(just bind to 127.0.0.1 and a random port) for some reason(cross-platform related) in my App. I use the posix function select to check if there is a new tcp connection. It works fine most time, but when I : 1) press home button, lock screen. 2) unlock screen, press App icon go back to App right away.
The select function returns, tells me I can handle my server socket, after I call accept function, it returns -1, and then I check the SO_ERROR by getsockopt, return the error EBADF. Codes below:
//listen fd is already nonblocking
if ((*pa = accept(*ps, addr, len)) != -1){
printf("after accept %d %d\n", *pa, errno);
return IO_DONE;
}
printf("after accept error %d %d %d %s\n", *ps, *pa, errno, strerror(errno));
int error = 0;
socklen_t len = sizeof (error);
int retval = getsockopt (*ps, SOL_SOCKET, SO_ERROR, &error, &len);
if (retval != 0) {
/* there was a problem getting the error code */
fprintf(stderr, "error getting socket error code: %s\n", strerror(retval));
}
if (error != 0) {
/* socket has a non zero error status */
fprintf(stderr, "socket error: %s\n", strerror(error));
}
log:
after accept error 5 -1 35 Resource temporarily unavailable
socket error: Bad file descriptor
My Test Device is a iPhone 4s, OS version is iOS 8.4.1
I have already https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/CommonPitfalls/CommonPitfalls.html and I have not found anything wrong.
So, Why this happen and how to solve?