While this is written in C , you should do the same in C# :
int dummy; // since you're just testing if the socket is still open for reading you don't // want it to block, or remove data from the sockets recv buffer int ret = recv(sockfd, &dummy, sizeof dummy, MSG_DONTWAIT | MSG_PEEK); if ( ( ret == -1 && ( errno == EAGAIN || errno == EWOULDBLOCK ) ) || ret > 0 ) { // socket is still open for reading else if ( ret == 0 ) { // socket went through orderly shutdown and is closed for reading } else { // there is some error and the socket is likely closed // check errno }
Say you think it is half closed and you still have the data you want to write.
int myData = 0xDEADBEEF; int ret = send(sockfd, myData, sizeof myData, MSG_NOSIGNAL);
Thus, if the socket is completely closed, your transfer will not kill your program with SIGPIPE . Instead, the call will simply return -1 and set errno to EPIPE .
source share