我在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功能。當我關閉套接字時,應用程序立即退出,沒有段錯誤或其他任何事情。我沒有看到這個問題,如果我不使用setNonBlocking函數。
如何在沒有此問題的情況下關閉無阻塞套接字?
在GDB下運行你的代碼。您描述的行爲(關閉導致應用程序退出而不崩潰)幾乎是不可能的。 – diciu 2009-12-29 10:08:17
您是否使用'socket()'函數創建了套接字?你的代碼中缺少這個。你聲明襪子(此時包含垃圾或0),然後你調用connect。 – Frunsi 2009-12-29 12:07:09
你能解釋一下你如何在代碼中發送/接收數據嗎?這可能會更有幫助。通常關閉不應該因爲套接字是非阻塞而不同。 – Jay 2009-12-29 18:21:26