2016-10-24 100 views
-2

什麼是阻斷髮送功能發送函數中最後一個參數是什麼意思?

void func1() //TCP 
{ 
    puts("FUNCTION:1\n"); 
    msq_server(); 
    if (send(connfd,(void *)&head,sizeof(head),0) < 0) { 
     printf("\n Error :TCP_sending error\n"); 
     exit(0); 
    } 
    puts("This is TCP packets Sending\n"); 
    sleep(1); 
} 
+0

究竟是什麼問題?是關於'send'的參數還是關於阻塞和非阻塞'send'的含義? – Prabhu

+0

它關於阻塞非阻塞和最後一個參數是標誌,所以它必須影響你的 – Nisarg

回答

0

默認情況下非阻塞,當您使用socket()套接字描述符,它是在阻塞模式。如果傳出的消息不適合套接字的發送緩衝區,則函數將阻塞調用線程,直到緩衝區中有足夠的空間被清除,或者直到發生超時/錯誤。

現在,您可以通過在創建套接字後將套接字標記爲非阻塞來覆蓋此阻塞行爲。這使得此後的任何操作(send()recv())都使用非阻塞模式。

如果您需要套接字處於阻止模式但具有特定的send()調用爲非阻塞,則可以使用MSG_DONTWAIT標誌啓用非阻塞操作。來自man-page of send()

MSG_DONTWAIT (since Linux 2.2) 
      Enables nonblocking operation; if the operation would block, 
      EAGAIN or EWOULDBLOCK is returned. This provides similar 
      behavior to setting the O_NONBLOCK flag (via the fcntl(2) 
      F_SETFL operation), but differs in that MSG_DONTWAIT is a per- 
      call option, whereas O_NONBLOCK is a setting on the open file 
      description (see open(2)), which will affect all threads in the 
      calling process and as well as other processes that hold file 
      descriptors referring to the same open file description. 
相關問題