TCP三次握手的過程,accept發生在三次握手的哪一個階段?

TCP三次握手的過程,accept發生在三次握手的哪一個階段?

答案是:accept過程發生在三次握手之後,三次握手完成後,客戶端和服務器就建立了tcp連接並可以進行數據交互了。這時可以調用accept函數獲得此連接。

也許這個圖描述的更加清晰。

Accept函數的原型是:

int accept(int socket, struct sockaddr *restrict address,socklen_t *restrict address_len);

功能描述的:

The accept() function shall extract the first connection on the queue of pending connections, create a new socket with the same socket type protocol and address family as the specified socket, and allocate a new file descriptor for that socket.

意思就是:accept函數會從已經建立連接的隊列中取出第一個連接,並創建一個新的socket,新的socket的類型和地址參數要和原來的那個指定的socket的地址一一樣,並且還要爲這個新的socket分配文件描述符。

The accepted socket cannot itself accept more connections. The original socket remains open and can accept more connections.

新建的這個socket自身是無法再接收連接了,但是最開始的那個socket仍然是處於開放狀態,而且可以接收更多連接。

If the listen queue is empty of connection requests and O_NONBLOCK is not set on the file descriptor for the socket, accept() shall block until a connection is present. If the listen() queue is empty of connection requests and O_NONBLOCK is set on the file descriptor for the socket, accept() shall fail and set errno to [EAGAIN] or [EWOULDBLOCK].

意思就是:在連接的監聽隊列爲空並且O_NONBLOCK 沒有置位的情況下,accpet是阻塞的。如果監聽隊列爲空,但是O_NONBLOCK 置位的情況下,accpet會立即返回。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章