幼敏悟过人,读书辄成诵。这篇文章主要讲述VB.net Socket Udp收发数据包示例源码相关的知识,希望能为你提供帮助。
【VB.net Socket Udp收发数据包示例源码】
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.Threading
Public Class Form1
Dim PortNumber As Integer = 39192 侦听端口号
Dim ListenerSock As Socket 侦听socket
Dim ListenerThre As Thread 侦听线程
Dim LocalIp As String本地ip64
Dim ready As Boolean = False 线程运行标识
Dim machinnos As String
Delegate Sub Gxdjs(ByVal data As String)线程内更新UI传送一个显示参数
Delegate Sub EditUi(ByVal data0 As String, ByVal data1 As String)线程内更新UI传送两个参数
Private Sub Form1_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
ListenerSock.Close()
ready = False
ListenerThre.Abort()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
getIp()
StartListener() 开始侦听
End Sub
Private Sub StartListener()
Dim LocalPoint As IPEndPoint
While Not ready 向用户询问侦听端口号。用户可以直接回车,表示选择默认的。
Try
LocalPoint = New IPEndPoint(IPAddress.Parse(LocalIp), PortNumber)
ListenerSock = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
ListenerSock.Bind(LocalPoint)
ListenerThre = New Thread(AddressOf ThrListener)
ListenerThre.Start()
ready = True
Catch ex As Exception
ListenerSock.Close()
ready = False
ListBox1.Items.Add("ERROR:" &
vbCrLf &
ex.Message &
vbCrLf)
End Try
End While
End Sub
Public Sub getIp()获取本机所有网卡的IP
Dim Address() As System.Net.IPAddress
Dim i As Integer
Address = Dns.GetHostByName(Dns.GetHostName()).AddressList
If UBound(Address) <
0 Then
MsgBox("未能查找到本台电脑安装的网卡,暂不能启动本软件。", MsgBoxStyle.Critical + vbOKOnly, "注意")
End
Else
For i = 0 To UBound(Address)
ComboBox1.Items.Add(Address(i).ToString())
Next
ComboBox1.SelectedIndex = 0
LocalIp = ComboBox1.Text.Trim()
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
ListenerSock.Close()
ready = False
ListenerThre.Abort()
LocalIp = ComboBox1.Text.Trim()
StartListener()开始侦听已选网卡的UDP端口
Catch
End Try
End Sub
Private Sub ThrListener() 侦听线程
While ready
Try
Dim bytes(1024) As Byte
Dim dataArray() As String
Dim RemotePoint As System.Net.EndPoint = New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
Dim NumGet As Integer
Dim Msg As String
Dim Sendinf As String
Dim SendBuf As Byte()
Dim DevBufferIpAddrStr As String
Dim DevBufferRemoteAddrStr As String
Dim DevBufferUseTimeStr As String
Dim DevRecFramesStr As String
Dim DevBufferMachinStr As String
Dim DevBufferCardidStr As String
Dim DevBufferUseMoneryStr As String
Dim DevBufferSerialNumStr As String
Dim ipep As IPEndPoint
NumGet = ListenerSock.ReceiveFrom(
推荐阅读
- C# TCP通讯客户端源码
- [恶意代码分析]恶意代码种类以及分析环境介绍
- 澳大利亚科学家开发出可用于脑机接口的新型碳基生物传感器
- ZooKeeper源码阅读心得分享+源码基本结构+源码环境搭建
- 4年4篇顶刊顶会论文 清华开源肖像线条画生成代码,惟妙惟肖模仿人类作画
- Google Research等机构提出新的AI算法以了解人脑网络中的电刺激效应
- 一个 3D 多功能和灵巧的神经界面
- 人工智能领域有哪些曾被拒稿的优秀工作()
- ZooKeeper客户端源码——向服务端发起请求(顺序响应+同步阻塞+异步回调)