[英]closing non-blocking socket
我在C中有以下代碼。
void setNonBlocking(SOCKET fd){
int flags;
if (-1 == (flags = fcntl(fd, F_GETFL, 0)))
flags = 0;
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
int main(){
int sock;
connect(sock, .....);
setNonBlocking(sock);
....
close(sock);
//we will do something here but the application exits in/after the close operation
}
我在具有setNonBlocking函數的非阻塞模式下使用套接字。 當我關閉套接字時,應用程序立即退出,而沒有segfault或其他任何內容。 如果我不使用setNonBlocking函數,則不會看到此問題。
沒有這個問題,我該如何關閉非阻塞套接字?
也許您的應用程序正在使用SIGPIPE。 使用套接字編程時,通常應處理或忽略SIGPIPE信號。
您將忽略fcntl
任何錯誤結果。 如果fcntl
返回-1,則至少應打印出一條錯誤消息(例如,使用perror
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.