用 vb.net socket通信
我們提供的服務有:成都做網(wǎng)站、成都網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、鐘山ssl等。為超過千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術(shù)的鐘山網(wǎng)站制作公司
Dim th As Threading.Thread
2 Dim tcpl As System.Net.Sockets.TcpListener
3
4 Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
5 th = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MyListen))
6 th.Start()
7 End Sub
8
9 Public Sub SendMessage()Sub SendMessage(ByVal IP As String, ByVal SendMsg As String)
10 Try
11 If IP "" Then
12 Dim tcpc As New System.Net.Sockets.TcpClient(IP, 5656)
13 Dim tcpStream As Net.Sockets.NetworkStream = tcpc.GetStream
14 Dim reqStream As New IO.StreamWriter(tcpStream)
15 reqStream.Write(SendMsg)
16 reqStream.Flush()
17 tcpStream.Close()
18 tcpc.Close()
19 End If
20 Catch ex As Exception
21 MsgBox(ex.Message.ToString)
22 End Try
23 End Sub
24 Private Sub MyListen()Sub MyListen()
25 Try
26 Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName).AddressList(0)
27 tcpl = New System.Net.Sockets.TcpListener(ipAddress, 5656)
28 tcpl.Start()
29 While True
30 Dim s As System.Net.Sockets.Socket = tcpl.AcceptSocket()
31 Dim MyBuffer(1024) As Byte
32 Dim i As Integer
33 i = s.Receive(MyBuffer)
34 If i 0 Then
35 Dim lstrRec As String
36 Dim j As Integer
37 For j = 0 To i - 1
38 TextBox1.Text += Chr(MyBuffer(j)) ","
39 Next
40 End If
41 End While
42 Catch ex As Exception
43 MsgBox(ex.Message.ToString)
44 End Try
45 End Sub
46
47 Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
48 SendMessage("192.168.0.61", TextBox2.Text)
49 End Sub
另起一個線程,在里面循環(huán)receive,每收到一個數(shù)據(jù)就進行分析,特殊命令如exit,執(zhí)行udpclient.close(),其他數(shù)據(jù)委托到主線程。
Public Function webCaptureContent(ByVal mWebsiteUrl As String, ByVal mWebsiteType As Boolean) As String
'啟動一次具體的數(shù)據(jù)采集工作,返回采集到的HTML內(nèi)容:要求必須輸入帶://的全地址數(shù)據(jù)
On Error Resume Next
Dim Str_WebContent As String = "請輸入查找網(wǎng)站地址."
Dim wb As WebClient = New WebClient() '//創(chuàng)建一個WebClient實例
If mWebsiteUrl.IndexOf("://") 0 Then
'//獲取或設置用于對向 Internet 資源的請求進行身份驗證的網(wǎng)絡憑據(jù)。(可有可無)
wb.Credentials = CredentialCache.DefaultCredentials
'//從資源下載數(shù)據(jù)并返回字節(jié)數(shù)組。(加@是因為網(wǎng)址中間有"/"符號)
Dim pagedata As Object = wb.DownloadData(mWebsiteUrl)
'//轉(zhuǎn)換字符
If mWebsiteType Then
Str_WebContent = Encoding.Default.GetString(pagedata)
Else
Str_WebContent = Encoding.UTF8.GetString(pagedata)
End If
End If
Return Str_WebContent '提取出來新聞內(nèi)容,刪除Body前后的多余內(nèi)容,同時補充上該 Body標記,形成完整的內(nèi)容 Str_WebContent '
End Function
不知道你是不是用51單片機往串口發(fā)數(shù)據(jù),是的話那是因為51的SBUF最多存放8個字節(jié),所以你發(fā)12個字節(jié)會分成兩次發(fā)完。還有在接收的時候最好定義好通信協(xié)議,比如加一個偵頭,一個偵尾,通過這兩個字節(jié)的內(nèi)容來接收數(shù)據(jù)。
老兄,提問還是給點懸賞分嘛,不要吝嗇哪點分,知識是無價的,你給得越多,回答你的人才會越多。
網(wǎng)頁標題:vb.net接收網(wǎng)絡數(shù)據(jù) vbnet webclient
網(wǎng)站地址:http://jinyejixie.com/article40/dosgdeo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供、移動網(wǎng)站建設、企業(yè)網(wǎng)站制作、小程序開發(fā)、建站公司、自適應網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)