WinSock控件能夠通過UDP協議(用戶數據報協議)或TCP協議(數據傳輸協議)連接到遠程的機器并進行數據交換。這兩種協議都能用來創建客戶端和服務端應用程序。就像定時器控件一樣,WinSock控件運行時沒有一個可視的界面。
可能的用途
創建客戶端應用程序,它能在信息到達中央服務器之前把用戶的信息收集起來。
創建服務端應用程序,它能作為來自多個用戶的數據一個集中處理點。
創建“聊天”程序。
協議的選擇
當我們使用WinSock控件時,首先要確定的是使用TCP還是UDP協議。它們之間主要的區別在于連接狀態:
TCP協議控件是一個基于連接的協議,就像電話機一樣,用戶必須在通話之前建立連接;
UDP是一個無連接的協議,兩臺計算機之間的事務處理就像傳紙條一樣:一臺計算機向另一臺計算機發送消息,但是它們之間并沒有一個明確的連接路徑。另外,發送的單個信息量的大小取決于網絡。
通常,你要創建的應用程序的類別就決定了你要選擇的協議。以下是幾個能夠幫助你選擇合適的協議的問題:
當發送或接收數據時,該應用程序需要從服務端或客戶端獲得認證嗎?如果要的話,那么TCP協議就正好需要在發送或接受數據前建立明確的連接。
要發送的數據量大嗎?(就像圖片、聲音文件之類)一旦建立了連接,TCP協議就會保持連接并保證數據的完整性。但是,這種連接會占用的更多的處理器資源,成本也會更高一些。
數據是陸續傳輸的,還是一次全部傳完呢?比如,如果你要創建的應用程序在某些任務完成時會告知具體的計算機,那么選擇UDP協議會更合適一些。UDP協議也更適合于發送小量數據。
協議的配置
配置你的應用程序所用到的協議:在設計階段,單擊工具窗口里的協議,選擇sckTCPProtocol或sckUDPProtocol。你也可以在代碼里配置協議,就像下面這樣:
Winsock1.Protocol=sckTCPProtocol
確定你的計算機名
要連接到遠程的計算機,你必須知道它的IP地址或別名。IP地址是一串用句點分隔的3位數字。通常,計算機的別名更容易讓人記住。
按下面的步驟可以找到你的計算機名:
在“任務欄”里單擊“開始”
在“設置”選項里單擊“控制面板”;
雙擊“網絡”圖標;
單擊“網絡標識”
在“計算機名”中顯示的就是你的計算機名。
一旦你找到你的計算名,它就可以作為遠程主機的屬性來用了。
TCP連接入門
當用TCP控件創建應用程序的時候,必須首先明確你的程序是作為服務端還是客戶端。創建服務端程序就意味著你的程序能夠在指定的端口進行“監聽”,而客戶端則能夠提出請求,服務端能夠接受請求并實現連接。一旦連接建立起來,客戶端和服務端就能夠自由地進行通信。
創建服務端程序
下面是創建一個簡單服務端程序的步驟:
創建一個標準EXE工程;
把默認窗體(Default form)的名字改為frmServer;
把form的標題(caption)改為TCP Server;
把Winsock控件拉到窗體中,并命名為tcpServer;
在窗體中添加2個文本框,分別命名為txtSendData和txtOutput‘
在窗體中加入下列代碼;
Private Sub Form_Load() ' Set the LocalPort property to an integer. ' Then invoke the Listen method. tcpServer.LocalPort = 1001 tcpServer.Listen frmClient.Show ' Show the client form. End Sub Private Sub tcpServer_ConnectionRequest _ (ByVal requestID As Long) ' Check if the control's State is closed. If not, ' close the connection before accepting the new ' connection. If tcpServer.State <> sckClosed Then _ tcpServer.Close ' Accept the request with the requestID ' parameter. tcpServer.Accept requestID End Sub Private Sub txtSendData_Change() ' The TextBox control named txtSendData ' contains the data to be sent. Whenever the user ' types into the textbox, the string is sent ' using the SendData method. tcpServer.SendData txtSendData.Text End Sub Private Sub tcpServer_DataArrival _ (ByVal bytesTotal As Long) ' Declare a variable for the incoming data. ' Invoke the GetData method and set the Text ' property of a TextBox named txtOutput to ' the data. Dim strData As String tcpServer.GetData strData txtOutput.Text = strData End Sub |
Private Sub Form_Load() ' The name of the Winsock control is tcpClient. ' Note: to specify a remote host, you can use ' either the IP address (ex: "121.111.1.1") or ' the computer's "friendly" name, as shown here. tcpClient.RemoteHost = "RemoteComputerName" tcpClient.RemotePort = 1001 End Sub Private Sub cmdConnect_Click() ' Invoke the Connect method to initiate a ' connection. tcpClient.Connect End Sub Private Sub txtSendData_Change() tcpClient.SendData txtSend.Text End Sub Private Sub tcpClient_DataArrival _ (ByVal bytesTotal As Long) Dim strData As String tcpClient.GetData strData txtOutput.Text = strData End Sub |
Private intMax As Long Private Sub Form_Load() intMax = 0 sckServer(0).LocalPort = 1001 sckServer(0).Listen End Sub Private Sub sckServer_ConnectionRequest _ (Index As Integer, ByVal requestID As Long) If Index = 0 Then intMax = intMax + 1 Load sckServer(intMax) sckServer(intMax).LocalPort = 0 sckServer(intMax).Accept requestID Load txtData(intMax) End If End Sub |
UDP連接入門
創建一個UDP應用程序比創建TCP程序更簡單,因為UDP協議不需要一個確定的連接。在上面的TCP應用程序中,其中一個Winsock控件必須明確的被設置為“監聽”,而另一個必須用連接方法發起連接。
相反,UDP協議不需要明確的連接。要在2個控件之間傳送數據,(連接的雙方)必須完成三個步驟:
確定遠程主機屬性為對方的計算機名;
確定遠程主機屬性為第二個控件的本地端口屬性;
調用約定方法指定要被使用的本地端口。(下面將詳細討論該方法)
創建一個的UDP連接端
創建一個標準EXE工程;
將默認窗體命名為frmPeerA;
在窗體中添加一個Winsock控件,命名為udpPeerA;
在屬性(Properties)頁,單擊協議(Protocol),改為UDPProtocol;
添加2個文本框控件窗體中,分別命名為txtSend和txtOutput;
在窗體中添加下面代碼:
Private Sub Form_Load() ' The control's name is udpPeerA With udpPeerA ' IMPORTANT: be sure to change the RemoteHost ' value to the name of your computer. .RemoteHost= "PeerB" .RemotePort = 1001 ' Port to connect to. .Bind 1002 ' Bind to the local port. End With frmPeerB.Show ' Show the second form. End Sub Private Sub txtSend_Change() ' Send text as soon as it's typed. udpPeerA.SendData txtSend.Text End Sub Private Sub udpPeerA_DataArrival _ (ByVal bytesTotal As Long) Dim strData As String udpPeerA.GetData strData txtOutput.Text = strData End Sub |
Private Sub Form_Load() ' The control's name is udpPeerB. With udpPeerB ' IMPORTANT: be sure to change the RemoteHost ' value to the name of your computer. .RemoteHost= "PeerA" .RemotePort = 1002 ' Port to connect to. .Bind 1001 ' Bind to the local port. End With End Sub Private Sub txtSend_Change() ' Send text as soon as it's typed. udpPeerB.SendData txtSend.Text End Sub Private Sub udpPeerB_DataArrival _ (ByVal bytesTotal As Long) Dim strData As String udpPeerB.GetData strData txtOutput.Text = strData End Sub |
原文轉自:http://www.anti-gravitydesign.com