作者:朱金燦
來源:blog.csdn.net/clever101
如何理解Windows Socket?
我的理解:Windows Socket相當於Windows平台上兩個網路實體開闢的一條通訊通道,這條通訊通道的實現細節對網路雙方是不可見的(呵呵,你就知道它是Windows製造)。通過這條通道,網路雙方可以傳輸各種資料。
你可能急不可待地問這條通道是怎樣開闢的呢?下面我以伺服器段和用戶端作為網路雙方進行說明。請看:
這樣你看明白了嗎?伺服器和用戶端之間的串連實際上是伺服器端Socket和用戶端Socket之間的串連。那麼在程式中具體如何呢?實際上很簡單,就是在伺服器端程式建立一個Socket類對象,在用戶端程式建立一個Socket類對象,然後把串連、資料轉送等網路通訊等工作交給他倆去做。
瞭解了Socket通訊模型,你是不是覺得Windows Socket網路編程不像想象中那麼難呢?然後你可以去瞭解MFC中的Socket類CAsyncSocket和CSocket類的用法,建議你重點看看那篇《Windows Sockets:
Socket Notifications》以及下面的
宋體;mso-font-kerning:0pt">• Windows
Sockets: Using Class CAsyncSocket
宋體;mso-font-kerning:0pt">• Windows
Sockets: Deriving from Socket Classes
宋體;mso-font-kerning:0pt">• Windows
Sockets: How Sockets with Archives Work
宋體;mso-font-kerning:0pt">• Windows
Sockets: Blocking
宋體;mso-font-kerning:0pt">• Windows
Sockets: Byte Ordering
宋體;mso-font-kerning:0pt">• Windows
Sockets: Converting Strings
mso-bidi-font-family:宋體;mso-font-kerning:0pt"> mso-bidi-font-family:宋體;mso-font-kerning:0pt">其實MFC中的Socket類和CWnd類有一點是相似的,就是有一些訊息激發函數。你需要理解什麼條件下這些函數會響應。最後我建議你看懂《Windows Sockets: Socket Notifications》的常式(點擊Samplemso-hansi-font-family:"Times New Roman"'>連結),那是一個簡單的聊天程式,這對理解Socketmso-hansi-font-family:"Times New Roman"'>很有好處(我就是從那得到啟發的)。