大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
用 vb.net socket通信
我们提供的服务有:成都做网站、成都网站制作、微信公众号开发、网站优化、网站认证、钟山ssl等。为超过千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的钟山网站制作公司
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
另起一个线程,在里面循环receive,每收到一个数据就进行分析,特殊命令如exit,执行udpclient.close(),其他数据委托到主线程。
Public Function webCaptureContent(ByVal mWebsiteUrl As String, ByVal mWebsiteType As Boolean) As String
'启动一次具体的数据采集工作,返回采集到的HTML内容:要求必须输入带://的全地址数据
On Error Resume Next
Dim Str_WebContent As String = "请输入查找网站地址."
Dim wb As WebClient = New WebClient() '//创建一个WebClient实例
If mWebsiteUrl.IndexOf("://") 0 Then
'//获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。(可有可无)
wb.Credentials = CredentialCache.DefaultCredentials
'//从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)
Dim pagedata As Object = wb.DownloadData(mWebsiteUrl)
'//转换字符
If mWebsiteType Then
Str_WebContent = Encoding.Default.GetString(pagedata)
Else
Str_WebContent = Encoding.UTF8.GetString(pagedata)
End If
End If
Return Str_WebContent '提取出来新闻内容,删除Body前后的多余内容,同时补充上该 Body标记,形成完整的内容 Str_WebContent '
End Function
不知道你是不是用51单片机往串口发数据,是的话那是因为51的SBUF最多存放8个字节,所以你发12个字节会分成两次发完。还有在接收的时候最好定义好通信协议,比如加一个侦头,一个侦尾,通过这两个字节的内容来接收数据。
老兄,提问还是给点悬赏分嘛,不要吝啬哪点分,知识是无价的,你给得越多,回答你的人才会越多。