用 vb.net socket通信
創(chuàng)新互聯(lián)專注于網(wǎng)站建設(shè)|網(wǎng)站維護(hù)|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計(jì)與制作經(jīng)驗(yàn),為許多企業(yè)提供了網(wǎng)站定制設(shè)計(jì)服務(wù),案例作品覆蓋成都紗窗等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身制作品質(zhì)網(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
都可以,可以用winsock,也可以自己重寫SOCKET套接字.建議你如果工期緊,可以先用WINSOCK.當(dāng)然最好是自己學(xué)用SOCKET套接字.這對(duì)未來開發(fā)網(wǎng)絡(luò)通訊是很有幫助的.因?yàn)橛肳INSOCK有個(gè)毛病就是殺毒軟件總會(huì)阻止
新建模塊
Function IsHearOK(ByVal URL As String) As Boolean '判斷網(wǎng)頁是否存在函數(shù)
Dim XMLObject As Object, ReturnType As String
XMLObject = CreateObject("Microsoft.XMLHTTP")
XMLObject.Open("GET", URL, False)
XMLObject.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded")
XMLObject.setRequestHeader("Range", "bytes=1-255")
Try
XMLObject.Send()
If XMLObject.Status = 200 Or XMLObject.Status = 206 Then
ReturnType = XMLObject.getResponseHeader("CONTENT-TYPE")
If UCase(ReturnType) "TEXT/HTML" Then
IsHearOK = True
Else
IsHearOK = False
End If
Else
IsHearOK = False
End If
Catch
MsgBox("連接異常,請檢查網(wǎng)絡(luò)!", , "提醒")
XMLObject = Nothing
End Try
End Function
調(diào)用 ishearok(url)=true 正常
Microsoft.VisualBasic.Devices.Network.IsAvailable As Boolean
生成實(shí)例后可以通過此屬性來獲取是否聯(lián)網(wǎng)的值,True代表已聯(lián)網(wǎng),F(xiàn)alse代表沒有聯(lián)網(wǎng)
Microsoft.VisualBasic.Devices.Network.NetworkAvailabilityChanged(ByVal Object, ByVal Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs)
該事件是在電腦斷開/連上互聯(lián)網(wǎng)是引發(fā)的
其中:
e.IsNetworkAvailable
的功能同Microsoft.VisualBasic.Devices.Network.IsAvailable
實(shí)例:
Dim?netListener?As?New?Microsoft.VisualBasic.Devices.Network?'生成實(shí)例
If?netListener.IsAvailable?=?True?Then
MsgBox("已聯(lián)網(wǎng)")
Else
MsgBox("未聯(lián)網(wǎng)")
End?If
NetworkAvailabilityChanged實(shí)例:
WithEvents?netListener?As?New?Microsoft.VisualBasic.Devices.Network?'生成實(shí)例
Private?Sub?OnNetworkChanged(ByVal?Object,?ByVal?Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs)Handles?netListener.NetworkAvailabilityChanged
If?e.IsNetworkAvailable?=?True?Then
MsgBox("已聯(lián)網(wǎng)")
Else
MsgBox("未聯(lián)網(wǎng)")
End?If
End?Sub
解決方法:
1.字符串內(nèi)容設(shè)置為定長,或者定義一個(gè)結(jié)束符(或字符串,如連續(xù)2個(gè)0xFF)附加在字符串尾部,程序以此方式先以文本方式讀取字符串?dāng)?shù)據(jù)。
2.字符串內(nèi)容也按照二進(jìn)制方式發(fā)送。
3.在傳輸?shù)淖址投M(jìn)制數(shù)據(jù)前附加N字節(jié)的標(biāo)識(shí)字節(jié),其中存儲(chǔ)發(fā)送的字符串和二進(jìn)制字符串長度。
方法很多。
網(wǎng)站標(biāo)題:vbnet網(wǎng)絡(luò) vbnet網(wǎng)絡(luò)編程
URL標(biāo)題:http://m.rwnh.cn/article32/doopppc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、品牌網(wǎng)站制作、企業(yè)建站、靜態(tài)網(wǎng)站、Google、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(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)
移動(dòng)網(wǎng)站建設(shè)知識(shí)