Socket連接過程

<pre name="code" class="objc">Socket的英文原義是“孔”或“插座”。作爲BSD UNIX的進程通信機制,取後一種意思。通常也稱作"套接字",用於描述IP地址和端口
<span style="color: rgb(51, 51, 51); font-family: arial, 宋體, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">在Internet上的</span><a target=_blank target="_blank" href="http://baike.baidu.com/view/23880.htm" style="text-decoration: none; color: rgb(19, 110, 194); font-family: arial, 宋體, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">主機</a><span style="color: rgb(51, 51, 51); font-family: arial, 宋體, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">一般運行了多個服務軟件,同時提供幾種服務。每種服務都打開一個Socket,並綁定到一個端口上,不同的端口對應於不同的服務。Socket正如其英文原意那樣,像一個多孔插座。一臺</span><a target=_blank target="_blank" href="http://baike.baidu.com/view/23880.htm" style="text-decoration: none; color: rgb(19, 110, 194); font-family: arial, 宋體, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">主機</a><span style="color: rgb(51, 51, 51); font-family: arial, 宋體, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">猶如佈滿各種插座的房間,每個插座有一個編號,有的插座提供220伏交流電, 有的提供110伏交流電,有的則提供有線電視節目。 客戶軟件將插頭插到不同編號的插座,就可以得到不同的服務。</span>
套接字之間的連接過程可以分爲三個步驟:服務器監聽,客戶端請求,連接確認。

(1)服務器監聽:是服務器端套接字並不定位具體的客戶端套接字,而是處於等待連接的狀態,實時監控網絡狀態。(2)客戶端請求:是指由客戶端的套接字提出連接請求,要連接的目標是服務器端的套接字。爲此,客戶端的套接字必須首先描述它要連接的服務器的套接字,指出服務器端套接字的地址和端口號,然後就向服務器端套接字提出連接請求。(3)連接確認:是指當服務器端套接字監聽到或者說接收到客戶端套接字的連接請求,它就響應客戶端套接字的請求,建立一個新的線程,把服務器端套接字的描述發給客戶端,一旦客戶端確認了此描述,連接就建立好了。而服務器端套接字繼續處於監聽狀態,繼續接收其他客戶端套接字的連接請求。

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