TCP協(xié)議是TCP/IP協(xié)議簇中的傳輸層中的一個(gè)協(xié)議,也是TCP/IP協(xié)議簇最為重要的協(xié)議之一。在TCP/IP協(xié)議簇中,有一個(gè)協(xié)議和TCP協(xié)議非常類似,這就是UDP協(xié)議,網(wǎng)絡(luò)上進(jìn)行基于UDP協(xié)議的數(shù)據(jù)傳送時(shí),發(fā)送方只需知道接收方的IP地址(或主機(jī)名)和端口號就可以發(fā)送UDP數(shù)據(jù)包。而接收方只需知道發(fā)送方發(fā)送數(shù)據(jù)對應(yīng)的端口號,就能夠接收UDP數(shù)據(jù)包了。傳送數(shù)據(jù)的雙方并不需要進(jìn)行連接就能夠?qū)崿F(xiàn)數(shù)據(jù)通訊,這樣就導(dǎo)致基于UDP協(xié)議的網(wǎng)絡(luò)應(yīng)用程序,在傳送數(shù)據(jù)時(shí)無法保證可靠性、完整性和安全性。
創(chuàng)新互聯(lián)長期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為甘南企業(yè)提供專業(yè)的做網(wǎng)站、成都網(wǎng)站設(shè)計(jì),甘南網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
而TCP協(xié)議卻與之相反,TCP協(xié)議是一種面向連接的,并提供可靠的、完整的、安全的數(shù)據(jù)傳送的網(wǎng)絡(luò)協(xié)議。它提供可靠字節(jié)服務(wù)的協(xié)議。在網(wǎng)絡(luò)中通過TCP協(xié)議傳送數(shù)據(jù)之前,發(fā)送方和接收方之間必須建立連接,這種連接就是所謂的"握手"。網(wǎng)絡(luò)中TCP應(yīng)用,如同日常生活中的打電話,在通話之前,首先要撥號、震鈴(猶如發(fā)送方向接收方提出TCP連接申請,并等待TCP連接申請通過)。直到對方拿起電話時(shí)(發(fā)送方和接收方的TCP連接已經(jīng)建立),就可以通話了(傳送數(shù)據(jù))。本文的主要內(nèi)容就來介紹在Visual Basic .Net實(shí)現(xiàn)基于TCP協(xié)議網(wǎng)絡(luò)數(shù)據(jù)傳送的一種簡單的方法。
一.簡介本文在實(shí)現(xiàn)TCP協(xié)議網(wǎng)絡(luò)應(yīng)用時(shí)使用的類庫:
.Net FrameWork SDK中提供了很多用以實(shí)現(xiàn)TCP協(xié)議或與之相關(guān)協(xié)議的類庫,本文就選擇五個(gè)比較典型,也是比較簡單的類加以介紹,即:TcpListener類、TcpClient類、NetworkStream類、StreamReader類和StreamWriter類。TcpClient主要用以提出TCP連接申請。TcpListener主要用以偵聽端口號,并接收遠(yuǎn)程主機(jī)的TCP連接申請。NetworkStream類是實(shí)現(xiàn)TCP數(shù)據(jù)傳輸?shù)幕A(chǔ)數(shù)據(jù)流,StreamReader類作用是通過操作NetworkStream,實(shí)現(xiàn)從網(wǎng)絡(luò)接收數(shù)據(jù)。StreamWriter類作用是通過操作NetworkStream,實(shí)現(xiàn)向網(wǎng)絡(luò)傳輸數(shù)據(jù)。
1. NetworkStream類:
NetworkStream類主要是提供用于網(wǎng)絡(luò)訪問的基礎(chǔ)數(shù)據(jù)流。它主要是網(wǎng)絡(luò)數(shù)據(jù)傳輸?shù)妮d體,并提供同步、異步方式來訪問網(wǎng)絡(luò)數(shù)據(jù)流。雖然NetworkStream類有構(gòu)造函數(shù),但在實(shí)際情況中更多是通過TcpClient實(shí)例的GetStream方法來初始化NetworkStream實(shí)例。以下就是使用TcpClient實(shí)例的GetStream方法來初始化NetworkStream實(shí)例具體代碼:
Dim tcpClient As TcpClient
Dim nsStream As NetworkStream
tcpClient = New TcpClient( "" , 8000)
''對遠(yuǎn)程主機(jī)的8000端口提出TCP連接申請
nsStream = tcpClient.GetStream ( )
''TCP連接建立后,獲得網(wǎng)絡(luò)數(shù)據(jù)傳輸?shù)幕A(chǔ)數(shù)據(jù)流
在下面介紹的程序示例中,就是利用NetworkStream作為傳送和接收數(shù)據(jù)的載體。而操作這個(gè)載體的就是StreamWriter類和StreamReader類。表01和表02是NetworkStream類中一些常用的方法、屬性及其說明。
方法 說明 BeginRead 開始異步讀者基礎(chǔ)數(shù)據(jù)流。 BeginWrite 開始異步寫入基礎(chǔ)數(shù)據(jù)流。 Close 關(guān)閉流并可選擇關(guān)閉基礎(chǔ)套接字。 EndRead 結(jié)束異步讀取。 EndWrite 結(jié)束異步寫入。 Flush 刷新流中的數(shù)據(jù)。 Read 從流中讀取數(shù)據(jù)。 Seek 將流的當(dāng)前位置設(shè)置為給定值。 SetLength 設(shè)置流的長度。 Write 將數(shù)據(jù)寫入流。
表01:NetworkStream類中常用的方法及其說明
其中"BeginRead"、"EndRead"和"BeginWrite"、"EndWrite"是二對異步方法,起作用分別相當(dāng)于"Read"和"Write"方法。
屬性 說明 CanRead 獲取當(dāng)前流是否支持讀取。 CanSeek 獲取流是否支持查找。該屬性總是返回 false。 CanWrite 獲取當(dāng)前流是否支持寫入。 DataAvailable 獲取是否可以在流上讀取數(shù)據(jù)。 Length 流上可用數(shù)據(jù)的長度。 Position 獲取或設(shè)置流中的當(dāng)前位置。
表02:NetworkStream類中屬性及其說明
2. StreamReader類:
StreamReader類能夠?qū)崿F(xiàn)對基礎(chǔ)數(shù)據(jù)流的讀操作,從而實(shí)現(xiàn)對經(jīng)過基礎(chǔ)數(shù)據(jù)流傳送來的數(shù)據(jù)。表03是StreamReader類的常用的方法及其說明:
方法 說明 Close 關(guān)閉StreamReader并釋放與閱讀器關(guān)聯(lián)的所有系統(tǒng)資源。 DiscardBufferedData 允許StreamReader丟棄其當(dāng)前數(shù)據(jù)。 Peek 返回下一個(gè)可用的字符,但不使用它。 Read 讀取輸入流中的下一個(gè)字符或下一組字符。 ReadBlock 從當(dāng)前流中讀取最大數(shù)量的字符并從索引開始將該數(shù)據(jù)寫入緩沖區(qū)。 ReadLine 從當(dāng)前流中讀取一行字符并將數(shù)據(jù)作為字符串返回。 ReadToEnd 從流的當(dāng)前位置到末尾讀取流。
表03:NetworkStream類中常用的方法及其說明
3. StreamWriter類:
StreamWriter類能夠?qū)崿F(xiàn)對基礎(chǔ)數(shù)據(jù)流的寫操作,從而實(shí)現(xiàn)提供基礎(chǔ)數(shù)據(jù)流來傳送數(shù)據(jù)。表04是StreamWriter類的常用方法及其說明:
方法 說明 Close 關(guān)閉當(dāng)前的StreamWriter和基礎(chǔ)流。 Flush 清理當(dāng)前編寫器的所有緩沖區(qū),并使所有緩沖數(shù)據(jù)寫入基礎(chǔ)流。 Write 寫入基礎(chǔ)數(shù)據(jù)流。 WriteLine 寫入重載參數(shù)指定的某些數(shù)據(jù),后跟行結(jié)束符。
表04:StreamWriter類的常用方法及其說明
4.TcpClient 類:
TcpClient 類主要為TCP網(wǎng)絡(luò)服務(wù)提供客戶端連接。TcpClient是類基于Socket類構(gòu)建,
它以更高的抽象程度提供TCP服務(wù)。TcpClient 提供了通過網(wǎng)絡(luò)連接、發(fā)送和接收數(shù)據(jù)的簡單方法。表05和表06分別是TcpClient類常用方法、屬性及其說明。
方法 說明 Close 關(guān)閉 TCP 連接 Connect 使用指定的主機(jī)名和端口號將客戶端連接到 TCP主機(jī) GetStream 返回用于發(fā)送和接收數(shù)據(jù)的流
表05:TcpClient類常用的方法
屬性 描述 LingerState 有關(guān)套接字逗留時(shí)間的信息 NoDelay 一個(gè)值,該值在發(fā)送或接收緩沖區(qū)未滿時(shí)啟用延遲 ReceiveBufferSize 接收緩沖區(qū)的大小 ReceiveTimeout TcpClient在啟動后為接收數(shù)據(jù)而等待的時(shí)間長度 SendBufferSize 發(fā)送緩沖區(qū)的大小 SendTimeout 在您啟動發(fā)送操作后TcpClient將為接收確認(rèn)而等待的時(shí)間長度
表06:TcpClient類常用的屬性
5.TcpListener 類:
TcpListener類的主要作用是從TCP網(wǎng)絡(luò)客戶端偵聽連接,TcpListener類基于Socket 類
提供更高理念級別的TCP服務(wù)。可以使用TcpListener從TCP客戶端偵聽連接。像 FTP 和 HTTP 這樣的應(yīng)用層協(xié)議是在 TcpListener 類的基礎(chǔ)上建立的。表7和表8分別是TcpListener類常用方法、屬性及其說明:
方法 說明 AcceptSocket 接受掛起的連接請求 AcceptTcpClient 接受掛起的連接請求 Pending 確定是否有掛起的連接請求 Start 開始偵聽網(wǎng)絡(luò)請求 Stop 關(guān)閉偵聽器表7:TcpListener 類常用的方法
屬性 說明 LocalEndpoint 獲取當(dāng)前TcpListener的基礎(chǔ)EndPoint Active 獲取一個(gè)值,該值指示 TcpListener 是否正主動偵聽客戶端連接 Server 獲取基礎(chǔ)網(wǎng)絡(luò)Socket
表8:TcpListener 類常用的屬性
二.Visual Basic .Net實(shí)現(xiàn)基于TCP協(xié)議數(shù)據(jù)傳送程序的體系結(jié)構(gòu):
在下面介紹的用Visual Basic .Net實(shí)現(xiàn)基于TCP協(xié)議的數(shù)據(jù)傳送程序是由二個(gè)子程序組成的。也可以看成是服務(wù)器端程序和客戶端程序,其中:服務(wù)器端程序的功能是偵聽端口號,接收遠(yuǎn)程主要的TCP連接申請,并接收遠(yuǎn)程主機(jī)傳送來的文字?jǐn)?shù)據(jù)。另外一個(gè)子程序,也就是所謂的客戶端程序,主要實(shí)現(xiàn)向網(wǎng)絡(luò)的遠(yuǎn)程主機(jī)提出TCP連接申請,并在連接申請通過后,向遠(yuǎn)程主機(jī)傳送文字?jǐn)?shù)據(jù)。下面來詳細(xì)介紹Visual Basic .Net實(shí)現(xiàn)TCP協(xié)議網(wǎng)絡(luò)數(shù)據(jù)傳送的服務(wù)器端程序和客戶端程序的具體步驟。
三.服務(wù)器端程序的具體實(shí)現(xiàn)步驟:
服務(wù)器端程序的實(shí)現(xiàn)關(guān)鍵在于偵聽端口號,接收遠(yuǎn)程主機(jī)的TCP連接申請,獲得網(wǎng)絡(luò)數(shù)據(jù)傳輸?shù)幕A(chǔ)數(shù)據(jù)流,并通過基礎(chǔ)數(shù)據(jù)流接收數(shù)據(jù)。接收數(shù)據(jù)使用的是StreamReader中ReadLine方法,由于ReadLine方法是一個(gè)阻塞式的方法,所以在下面具體的實(shí)現(xiàn)步驟中,是接收數(shù)據(jù)是在創(chuàng)建的線程中完成的,具體可參閱下面實(shí)現(xiàn)步驟中的第十一和十二步。以下是Visual Basic .Net實(shí)現(xiàn)TCP協(xié)議客戶端程序?qū)崿F(xiàn)的具體步驟:
1. 啟動Visual Studio .Net。
2. 選擇菜單【文件】|【新建】|【項(xiàng)目】后,彈出【新建項(xiàng)目】對話框。
3. 將【項(xiàng)目類型】設(shè)置為【Visual Basic項(xiàng)目】。
4. 將【模板】設(shè)置為【W(wǎng)indows應(yīng)用程序】。
5. 在【名稱】文本框中輸入【服務(wù)器端程序】。
6. 在【位置】的文本框中輸入【E:\VS.NET項(xiàng)目】,然后單擊【確定】按鈕,這樣在"E:\VS.NET項(xiàng)目"目錄中就產(chǎn)生了名稱為"服務(wù)器端程序"的文件夾,并在里面創(chuàng)建了名稱為"服務(wù)器端程序"的項(xiàng)目文件。
7. 把Visual Studio .Net的當(dāng)前窗口切換到【Form1.vb(設(shè)計(jì))】窗口,并從【工具箱】中的【W(wǎng)indows窗體組件】選項(xiàng)卡中往Form1窗體中拖入下列組件,并執(zhí)行相應(yīng)的操作:
一個(gè)Label組件。
一個(gè)StatusBar組件。
一個(gè)ListBox組件。
一個(gè)Button組件,并在這個(gè)Button組件拖入Form1的設(shè)計(jì)窗體后,雙擊它,則系統(tǒng)會在Form1.vb文件分別產(chǎn)生這個(gè)組件的Click事件對應(yīng)的處理代碼。
8. 按照表05所示調(diào)整窗體中各組件屬性的數(shù)值:
組件類型 組件名稱 屬性 設(shè)置結(jié)果 Form Form1 Text 服務(wù)器端程序 Form1 MaximizeBox False Form1 FormBorderStyle FixedSingle Button Button1 Text 啟動服務(wù) Button1 FlatStyle Flat Label Label1 Text 服務(wù)尚未啟動 StatusBar StatusBar1 Text 無連接!表05:【服務(wù)器端程序】項(xiàng)目中組件設(shè)定數(shù)值表
9. 把Visual Studio .Net的當(dāng)前窗口切換到Form1.vb的代碼編輯窗口,并在Form1.vb文件的最前面添加下列代碼,下列代碼在Form1.vb中導(dǎo)入程序中要使用的類所在的命名空間:
Imports System.Net.Sockets
''使用到TcpListen類
Imports System.Threading
''使用到線程
Imports System.IO
''使用到StreamReader類
10. 在Form1.vb中創(chuàng)建各種可視組件的代碼中添加下列代碼,下列代碼的作用是創(chuàng)建全局使用的實(shí)例和變量:
Private iPort As Integer = 8000
''定義偵聽端口號
Private thThreadRead As Thread
''創(chuàng)建線程,用以偵聽端口號,接收信息
Private tlTcpListen As TcpListener
''偵聽端口號
Private blistener As Boolean = True
''設(shè)定標(biāo)示位,判斷偵聽狀態(tài)
Private nsStream As NetworkStream
''創(chuàng)建接收的基本數(shù)據(jù)流
Private srRead As StreamReader
''從網(wǎng)絡(luò)基礎(chǔ)數(shù)據(jù)流中讀取數(shù)據(jù)
Private tcClient As TcpClient
11. 在Form1.vb中的InitializeComponent過程之后添加下列代碼,下列代碼的作用是定義Listen過程,此過程的作用是偵聽本地機(jī)的8000端口號,接受網(wǎng)絡(luò)主機(jī)的TCP連接申請,并接收從建立申請的遠(yuǎn)程主機(jī)發(fā)送來的文本數(shù)據(jù):
Private Sub Listen ( )
Try
tlTcpListen = New TcpListener ( iPort )
''以8000端口號來初始化TcpListener實(shí)例
tlTcpListen.Start ( )
''開始監(jiān)聽
StatusBar1.Text = "正在監(jiān)聽..."
tcClient = tlTcpListen.AcceptTcpClient ( )
''通過TCP連接請求
nsStream = tcClient.GetStream ( )
''獲取用以發(fā)送、接收數(shù)據(jù)的網(wǎng)絡(luò)基礎(chǔ)數(shù)據(jù)流
srRead = New StreamReader ( nsStream )
''以得到的網(wǎng)絡(luò)基礎(chǔ)數(shù)據(jù)流來初始化StreamReader實(shí)例
StatusBar1.Text = "已經(jīng)建立TCP連接!"
''循環(huán)偵聽
While blistener
Dim sMessage As String = srRead.ReadLine ( )
''從網(wǎng)絡(luò)基礎(chǔ)數(shù)據(jù)流中讀取一行數(shù)據(jù)
If ( sMessage = "STOP" ) Then
tlTcpListen.Stop ( )
''關(guān)閉偵聽
nsStream.Close ( )
srRead.Close ( )
''釋放資源
StatusBar1.Text = "無連接!"
thThreadRead.Abort ( )
''中止線程
Return
Else
''判斷是否為斷開TCP連接控制碼
Dim sTime As String = DateTime.Now.ToShortTimeString ( )
''獲取接收數(shù)據(jù)時(shí)的時(shí)間
ListBox1.Items.Add ( sTime + " " + sMessage )
End If
End While
Catch ex As System.Security.SecurityException
MessageBox.Show ( "偵聽失敗!" , "錯(cuò)誤" )
End Try
End Sub
12. 用下列代碼替換Form1.vb中的Button1的Click事件對應(yīng)的處理代碼,下列代碼功能是用上面定義的Listen過程來初始化并啟動線程,接收建立TCP連接的遠(yuǎn)程主機(jī)發(fā)送來的文本數(shù)據(jù):
Private Sub Button1_Click ( ByVal sender As System.Object , ByVal e As System.EventArgs ) Handles Button1.Click
thThreadRead = New Thread ( New ThreadStart ( AddressOf Listen ) )
''以Listen過程來初始化線程實(shí)例
thThreadRead.Start ( )
''啟動線程
Button1.Enabled = False
Label1.Text = "服務(wù)已經(jīng)啟動!"
Label1.ForeColor = Color.Red
End Sub
13. 用下列代碼替換Form1.vb中的Dispose過程,下面代碼的作用是重新定義Dispose過程,在Dispose過程手動清除使用的資源,回收垃圾:
Protected Overloads Overrides Sub Dispose ( ByVal disposing As Boolean )
Try
thThreadRead.Abort ( ) ''中止線程
tlTcpListen.Stop ( ) ''關(guān)閉偵聽
tcClient.Close ( )
nsStream.Close ( )
srRead.Close ( ) ''釋放資源
Catch
End Try
If disposing Then
If Not ( components Is Nothing ) Then
components.Dispose ( )
End If
End If
MyBase.Dispose ( disposing )
End Sub
14. 至此在上述步驟都正確執(zhí)行后,【服務(wù)器端程序】項(xiàng)目的全部工作就完成了。編譯、生成可執(zhí)行文件后,接著介紹客戶端程序的實(shí)現(xiàn)步驟。
四.客戶端端程序的具體實(shí)現(xiàn)步驟:
客戶端端序的實(shí)現(xiàn)關(guān)鍵在于向網(wǎng)絡(luò)中的遠(yuǎn)程主機(jī)提出TCP連接申請,并在申請通過后,得到傳輸數(shù)據(jù)的基礎(chǔ)數(shù)據(jù)流,并通過對基礎(chǔ)數(shù)據(jù)流進(jìn)行寫操作向遠(yuǎn)程主機(jī)傳送文本數(shù)據(jù)。由于在客戶端程序中沒有使用阻塞式的方法,所以程序中沒有使用到線程。對遠(yuǎn)程主機(jī)提出TCP連接申請的具體實(shí)現(xiàn)方法請參閱以下第步;對基礎(chǔ)數(shù)據(jù)流進(jìn)行寫操作,從而實(shí)現(xiàn)向遠(yuǎn)程主機(jī)傳送文本數(shù)據(jù)的方法請參閱以下第步。下面客戶端程序的具體實(shí)現(xiàn)步驟:
1. 啟動Visual Studio .Net。
2. 選擇菜單【文件】|【新建】|【項(xiàng)目】后,彈出【新建項(xiàng)目】對話框。
3. 將【項(xiàng)目類型】設(shè)置為【Visual Basic項(xiàng)目】。
4. 將【模板】設(shè)置為【W(wǎng)indows應(yīng)用程序】。
5. 在【名稱】文本框中輸入【客戶端程序】。
6. 在【位置】的文本框中輸入【E:\VS.NET項(xiàng)目】,然后單擊【確定】按鈕,這樣在"E:\VS.NET項(xiàng)目"目錄中就產(chǎn)生了名稱為"客戶端程序"的文件夾,并在里面創(chuàng)建了名稱為"客戶端程序"的項(xiàng)目文件。
7. 把Visual Studio .Net的當(dāng)前窗口切換到【Form1.vb(設(shè)計(jì))】窗口,并從【工具箱】中的【W(wǎng)indows窗體組件】選項(xiàng)卡中往Form1窗體中拖入下列組件,并執(zhí)行相應(yīng)的操作:
二個(gè)Label組件。
二個(gè)TextBox組件。
一個(gè)StatusBar組件。
二個(gè)Button組件,并在這二個(gè)Button組件拖入Form1的設(shè)計(jì)窗體后,雙擊它們,則系統(tǒng)會在Form1.vb文件分別產(chǎn)生這二個(gè)組件的Click事件對應(yīng)的處理代碼。
8. 按照表01所示調(diào)整窗體中各組件屬性的數(shù)值:
組件類型 組件名稱 屬性 設(shè)置結(jié)果 Form Form1 Text 客戶端程序 Form1 MaximizeBox False Form1 FormBorderStyle FixedSingle Button Button1 Text 連接 Button1 FlatStyle Flat Button2 Text 發(fā)送 Button2 FlatStyle Flat Label Label1 Text 服務(wù)器IP地址: Label2 Text 信息: StatusBar StatusBar1 Text 無連接! TextBox TextBox1 Text "" TextBox1 BorderStyle FixedSingle TextBox2 Text "" TextBox2 BorderStyle FixedSingle
表06:【客戶端程序】項(xiàng)目中組件設(shè)定數(shù)值表
有任何疑問請追問,滿意請采納,謝謝。
WNetAddConnection2 別名(alias):WNetAddConnection2A
WNetAddConnection2 庫名(library):mpr
WNetAddConnection2 操作系統(tǒng)(os):Requires Windows NT 3.1 or later; Requires Windows 95 or later
WNetAddConnection2 參數(shù)表(parameter):
lpNetResource -- NETRESOURCE,在這個(gè)結(jié)構(gòu)中設(shè)置了下述字段,對要連接的網(wǎng)絡(luò)資源進(jìn)行了定義:dwType,
lpLocalName (可為 vbNullString), lpRemoteName, lpProvider (設(shè)為 vbNullString
表示用默認(rèn)提供者)。該結(jié)構(gòu)的其他所有變量都會被忽略
lpPassword ----- String,可選的一個(gè)密碼。如為vbNullString,表示采用當(dāng)前用戶的默認(rèn)密碼。如為一個(gè)空字串,則不用任何密碼
lpUserName ----- String,用于連接的用戶名。如為vbNullString,表示使用當(dāng)前用戶
dwFlags -------- Long,設(shè)為零;或指定常數(shù)CONNECT_UPDATE_PROFILE,表示創(chuàng)建永久性連接
?lpNetResource
Points to a NETRESOURCE structure that specifies details of the proposed connection: information about the network resource, the local device, and the network resource provider.
You must specify the following members of the NETRESOURCE structure:
dwType
Specifies the type of network resource to connect to. If lpLocalName points to a non-empty string, this member can be RESOURCETYPE_DISK or RESOURCETYPE_PRINT. If lpLocalName is NULL or points to an empty string, dwType can be RESOURCETYPE_DISK, RESOURCETYPE_PRINT, or RESOURCETYPE_ANY.
lpLocalName
Points to a null-terminated string that specifies the name of a local device to be redirected, such as 揊:?or 揕PT1? The string is treated in a case-insensitive manner. If the string is empty or lpLocalName is NULL, the function makes a connection to the network resource without redirecting a local device.
lpRemoteName
Points to a null-terminated string that specifies the network resource to connect to. The string can be up to MAX_PATH characters in length. The string must follow the network provider抯 naming conventions.
lpProvider
Points to a null-terminated string that specifies the network provider to connect to. If lpProvider is NULL or points to an empty string, the operating system attempts to determine the correct provider by parsing the string pointed to by lpRemoteName.
You should set this member only if you know for sure which network provider you want to use. Otherwise, let the operating system determine which provider the network name maps to.
If this member is not NULL, the operating system attempts to make a connection only to the named network provider.
The WNetAddConnection2 function ignores the other members of the NETRESOURCE structure.
?lpPassword
Points to a null-terminated string that specifies a password to be used in making the network connection.
If lpPassword is NULL, the function uses the current default password associated with the user specified by lpUserName.
If lpPassword points to an empty string, the function does not use a password.
?lpUsername
Points to a null-terminated string that specifies a user name to be used in making the connection.
If lpUserName is NULL, the function uses the default user name. The user context for the process provides the default user name.
The lpUserName parameter is specified when users want to connect to a network resource for which they have been assigned a user name or account other than the default user name or account.
The user-name string represents a security context. It may be specific to a network provider.
?dwFlags
A set of bit flags that specify connection options. The following bit flag constant is currently defined:
CONNECT_UPDATE_PROFILE
The network resource connection should be remembered.
If this bit flag is set, the operating system automatically attempts to restore the connection when the user logs on.
The operating system remembers only successful connections that redirect local devices. It does not remember unsuccessful connections or deviceless connections. A deviceless connection occurs when lpLocalName is NULL or points to an empty string.
If this bit flag is clear, the operating system will not automatically restore the connection at logon.
WNetAddConnection2 返回值(return):
Long,零表示成功。會設(shè)置GetLastError。如GetLastError是ERROR_EXTENDED_ERROR,則可用WNetGetLastError取得額外的錯(cuò)誤信息
If the function succeeds, the return value is NO_ERROR.
If the function fails, the return value is an error code. Returning an error code provides compatibility with the behavior of the Windows 3.1 function WNetAddConnection. You can also call the GetLastError function to obtain the (same) error code. One of the following error codes may be returned when WNetAddConnection2 fails:
ERROR_ACCESS_DENIED
Access to the network resource was denied.
ERROR_ALREADY_ASSIGNED
The local device specified by lpLocalName is already connected to a network resource.
ERROR_BAD_DEV_TYPE
The type of local device and the type of network resource do not match.
ERROR_BAD_DEVICE
The value specified by lpLocalName is invalid.
ERROR_BAD_NET_NAME
The value specified by lpRemoteName is not acceptable to any network resource provider. The resource name is invalid, or the named resource cannot be located.
ERROR_BAD_PROFILE
The user profile is in an incorrect format.
ERROR_BAD_PROVIDER
The value specified by lpProvider does not match any provider.
ERROR_BUSY
The router or provider is busy, possibly initializing. The caller should retry.
ERROR_CANCELLED
The attempt to make the connection was cancelled by the user through a dialog box from one of the network resource providers, or by a called resource.
ERROR_CANNOT_OPEN_PROFILE
The system is unable to open the user profile to process persistent connections.
ERROR_DEVICE_ALREADY_REMEMBERED
An entry for the device specified in lpLocalName is already in the user profile.
ERROR_EXTENDED_ERROR
A network-specific error occured. Call the WNetGetLastError function to get a description of the error.
ERROR_INVALID_PASSWORD
The specified password is invalid.
ERROR_NO_NET_OR_BAD_PATH
A network component has not started, or the specified name could not be handled.
ERROR_NO_NETWORK
There is no network present.
本來可以用簡單的dos命令的,但是影射成功與否返回不理想,建議用API,見下面
Module?LocalNetConnect'模塊,直接復(fù)制即可
Public?Declare?Function?WNetAddConnection2?Lib?"mpr.dll"?Alias?"WNetAddConnection2A"?(ByRef?lpNetResource?As?NETRESOURCE,?ByVal?lpPassword?As?String,?ByVal?lpUserName?As?String,?ByVal?dwFlags?As?Integer)?As?Integer
Public?Declare?Function?WNetCancelConnection2?Lib?"mpr.dll"?Alias?"WNetCancelConnection2A"?(ByVal?lpName?As?String,?ByVal?dwFlags?As?Integer,?ByVal?fForce?As?Integer)?As?Integer
Public?Structure?NETRESOURCE
Dim?dwScope?As?Integer
Dim?dwType?As?Integer
Dim?dwDisplayType?As?Integer
Dim?dwUsage?As?Integer
Dim?lpLocalName?As?String
Dim?lpRemoteName?As?String
Dim?lpComment?As?String
Dim?lpProvider?As?String
End?Structure
Public?Function?netconnect(ByVal?localDrive?As?String,?ByVal?sharePath?As?String,?ByVal?userName?As?String,?ByVal?UserPassword?As?String)?As?Boolean
Dim?NetR?As?New?NETRESOURCE
Dim?ErrInfo?As?Integer
NetR.dwScope?=?2
NetR.dwType?=?1
NetR.dwDisplayType?=?3
NetR.dwUsage?=?1
'設(shè)置驅(qū)動器
NetR.lpLocalName?=?localDrive
'設(shè)置遠(yuǎn)程端口名字
NetR.lpRemoteName?=?sharePath
'設(shè)置連接
ErrInfo?=?WNetAddConnection2(NetR,?UserPassword,?userName,?1)
If?ErrInfo?=?0?Then
Return?True
Else
Return?False
End?If
End?Function
Public?Function?netdisconnect(ByVal?localDrive?As?String)?As?Boolean
Dim?ErrInfo?As?Integer
ErrInfo?=?WNetCancelConnection2(localDrive,?1,?False)
If?ErrInfo?=?0?Then
Return?True
Else
Return?False
End?If
End?Function
End?Module
‘調(diào)用
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
'這里是映射調(diào)用,administrator為用戶名,根據(jù)實(shí)際用戶名
’如果將“Z:”設(shè)置為空就不會網(wǎng)絡(luò)盤映射但是已經(jīng)和172.26.120.47聯(lián)機(jī)了(返回真時(shí)成立)。
If?netconnect("Z:",?"\\172.26.120.47\c$",?"administrator",?"局域網(wǎng)電腦實(shí)際密碼")?=?True?Then
MsgBox("success")
Else
MsgBox("Faile")
End?If
End?Sub
Private?Sub?Button2_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button2.Click'這里是取消映射
If?netdisconnect("Z:")?=?True?Then
MsgBox("success")
Else
MsgBox("Faile")
End?If
End?Sub
文件鏡像功能吧, 監(jiān)控文件的變化然后實(shí)時(shí)同步就行了 .. 好像是叫那個(gè)filewatcher類,可以做
首先來了解遠(yuǎn)程線程注入遠(yuǎn)程線程插入(注入)技術(shù)指的是通過在另一個(gè)進(jìn)程中創(chuàng)建遠(yuǎn)程線程的方法進(jìn)入目標(biāo)進(jìn)程的內(nèi)存地址空間。將木馬程序以DLL的形式實(shí)現(xiàn)后,需要使用插入到目標(biāo)進(jìn)程中的遠(yuǎn)程線程將該木馬DLL插入到目標(biāo)進(jìn)程的地址空間,即利用該線程通過調(diào)用Windows API LoadLibrary函數(shù)來加載木馬DLL,從而實(shí)現(xiàn)木馬對系統(tǒng)的侵害。 這種技術(shù)一般用于外掛 當(dāng)外掛注入到游戲中時(shí) 你的電腦也就中啦木馬 一般的解決方法 wmiprvse.exe是一個(gè)系統(tǒng)服務(wù)的進(jìn)程,你可以結(jié)束任務(wù),進(jìn)程自然消失。 禁用Windows Management Instrumentation Driver Extensions服務(wù)或者改為手動 具體:桌面-我的電腦-管理-服務(wù)和應(yīng)用程序-服務(wù) 里面有個(gè)Windows Management Instrumentation 右鍵—禁用就可以了. 我也用過,感覺第二種方法較好。 解除命令方法:同樣操作復(fù)制下邊的命[1][2][3]令粘貼輸入,回車確定。即可、 reg add “HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\wmiprvse.exe” /f希望會幫到你
名稱欄目:vb.net遠(yuǎn)程映射的簡單介紹
分享地址:http://chinadenli.net/article36/heiosg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、云服務(wù)器、標(biāo)簽優(yōu)化、域名注冊、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)