陳 翀
摘要在VB語(yǔ)言設(shè)計(jì)中 Winsock控件對(duì)用戶(hù)是不可視的,可以很容易地訪(fǎng)問(wèn)TCP和UDP網(wǎng)絡(luò)服務(wù)。要編寫(xiě)客戶(hù)和服務(wù)器應(yīng)用程序,不需要了解TCP或調(diào)用底層Winsock API的具體細(xì)節(jié)。通過(guò)設(shè)置Winsock控件的屬性和調(diào)用該控件的方法,可以很容易地連接到遠(yuǎn)程計(jì)算機(jī)并進(jìn)行雙向的數(shù)據(jù)交換。
關(guān)鍵詞VB Winsock
中圖分類(lèi)號(hào):TP3文獻(xiàn)標(biāo)識(shí)碼:A
1 VB語(yǔ)言的簡(jiǎn)介
20世紀(jì)70年代末,Microsoft在當(dāng)時(shí)的PC機(jī)上開(kāi)發(fā)了第一代的Basic語(yǔ)言,隨著Windows操作系統(tǒng)的不斷成熟,Visual Basic產(chǎn)品由1.0版升級(jí)到3.0版,此時(shí) Visual Basic已初具規(guī)模了。
隨著Internet的迅猛發(fā)展,Microsoft的ActiveX技術(shù)出現(xiàn)了,并被不失時(shí)機(jī)地加入到 Visual Basic 5.0版本中(1997年)。在1998年,Microsoft推出了Visual Basic 6.0版本,這一版本使VB得到了很大的擴(kuò)充和增強(qiáng)。它還引入了使用部件編程的概念。迄今為止,Visual Basic已經(jīng)發(fā)展成為快速應(yīng)用程序開(kāi)發(fā)工具的代表。
2 連接測(cè)試
此程序分別安裝在兩臺(tái)不同計(jì)算機(jī)上,并連接在LAN上,一臺(tái)為主機(jī),一臺(tái)為客戶(hù)機(jī)。主機(jī)一直監(jiān)聽(tīng)所設(shè)定的端口??蛻?hù)機(jī)上設(shè)主機(jī)的IP,設(shè)好主機(jī)端口,點(diǎn)擊連接鍵,客戶(hù)機(jī)就與主機(jī)連接。并可互相發(fā)送信息。
2.1 設(shè)法連接主機(jī)
Private Sub cmdConnnect_Click()
cmdListen.Enabled = False
WinsockConnectTest.Close
WinsockConnectTest.Connect txtIPaddress.Text, txtTestPort
lblConnectionStatus.Caption = "通過(guò)端口 " & txtTestPort.Text & " 連接 " & txtIPaddress.Text & "<|||>" & "Connecting to " & txtIPaddress.Text & " on port " & txtTestPort.Text
Timer1.Enabled = True
End Sub
2.2 監(jiān)聽(tīng)試圖連接你的主機(jī)
Private Sub cmdListen_Click()cmdConnnect.Enabled = False
WinsockConnectTest.Close
WinsockConnectTest.LocalPort = txtTestPort.Text
WinsockConnectTest.Listen
lblConnectionStatus.Caption = "正在監(jiān)聽(tīng)/Listening on port " & txtTestPort.Text
End Sub
2.3 檢查網(wǎng)絡(luò)是否連接
Private Sub lblCheckConnection_Click()
If Not WinsockConnectTest.State = 7 Then
lblCheckConnection.Caption = "連接/Connected: 否/No"
Else
lblCheckConnection.Caption = "連接/Connected: 是/Yes"
End If
End Sub
2.4 當(dāng)客戶(hù)機(jī)試圖連接主機(jī)時(shí),程序?qū)㈥P(guān)注連接狀態(tài)
Private Sub Timer1_Timer() '
If Not WinsockConnectTest.State = 7 Then
lblConnectionStatus.Caption = "連接失敗/Connection Failed"
WinsockConnectTest.Close
cmdConnnect.Enabled = True
cmdListen.Enabled = True
Timer1.Enabled = False
Else
If cmdListen.Enabled = True Then
lblConnectionStatus.Caption = "連接/Connected"
End If
End If
End Sub
2.5 當(dāng)連接時(shí),WinsockConnectTest.SendData將數(shù)據(jù)發(fā)送出去
Private Sub cmdSendDataServer_Click()
If WinsockConnectTest.State = 7 Then
Dim SendDataServer As String
SendDataServer = txtSendData.Text
WinsockConnectTest.SendData SendDataServer
Else
Beep
End If
End Sub
2.6 當(dāng)接收到數(shù)據(jù)時(shí),WinsockConnectTest.GetData將數(shù)據(jù)轉(zhuǎn)為字符,并顯示
Private Sub WinsockConnectTest_DataArrival(ByVal bytesTotal As Long)
Dim ServerData As String
WinsockConnectTest.GetData ServerData, (下轉(zhuǎn)第186頁(yè))(上接第182頁(yè))vbString
txtDataServer.SelStart = Len(txtDataServer.Text)
txtDataServer.SelText = ServerData & vbCrLf
End Sub
3 端口掃描
本程序的這個(gè)功能為掃描當(dāng)前所打開(kāi)的端口,并將其端口號(hào)顯示在列表中。在“主機(jī)IP地址”框中設(shè)置好要主機(jī)IP。選擇是本地還是互聯(lián)網(wǎng)掃描,設(shè)置所要掃描端口的范圍和掃描速度。點(diǎn)擊掃描運(yùn)行程序:
掃描端口主要代碼
Private Sub CmdScan_Click()
PortLow = txtPortLow.Text '起始端口號(hào)
PortHigh = txtPortHigh.Text '中止端口號(hào)
TimerScanner.Enabled = False
If Pause = True Then
lblShowsatwhatPort.Caption = "端口號(hào)/Port: " & AtPort & " 暫停/Pause"
Exit Sub
End If
If Start = "No" Then
Exit Sub
End If
If AtPort = PortHigh Then '檢查是否掃描到所設(shè)定的最后一個(gè)端口:
Exit Sub
End If
CmdScan.Enabled = False
If optLocal.Value = True Then '如果這個(gè)為本地掃描,另一個(gè)則為互聯(lián)網(wǎng)掃描:
Do
If Start = "No" Then
Exit Sub
End If
WinsockPortScanner.Close
DoEvents
WinsockPortScanner.Connect txtIPHostscanner.Text, AtPort
DoEvents
參考文獻(xiàn)
[1] 龔沛曾,陸慰民,楊志強(qiáng).Visual Basic程序設(shè)計(jì)教程.高等教育出版社,2000.7.
[2] 鄧文新,張桂香.Visual Basic程序設(shè)計(jì)方法.北京航空航天大學(xué)出版社,2003.1.