VB.NET C#实现基于UDP的免服务器局域网多客户端点对点通讯

前端之家收集整理的这篇文章主要介绍了VB.NET C#实现基于UDP的免服务器局域网多客户端点对点通讯前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

相信很多人都用过类似飞秋的软件,过去也做过很多局域网的数据传输应用,但要么就是基于TCP的,要么就是需要用户设置对方IP的,需要用户对计算机网络及IP地址参数有一定的认知能力。

所以抽时间我重新封装了一个基于UDP的局域网通讯类库,该类库具有局域网内同一网段自我发现及互相注册能力,可以与指定的客户端进行点对点通讯,客户端属于平行关系,无客户端与服务器的关系。

通过此类库,我们可以快速部署一个UDP通讯程序,而无需用户对IP地址等参数进行设置就可找到客户端。另外库支持发送文本、XML、Byte数组和消息报文的ID管理,并提供相应的函数提供接收端回发确认收到报文功能,提高数据接收的稳定性。通过发送XML,利用此类库开发的程序员可以根据自己的协议发送任意数据,包括udp文件传送等。

以下是Demo程序的部分代码,需要类库的可在我的资源下载中下载。

Imports IntarnetUdpLibrary.DeviceFinder

Public Class frmMain
Public WithEvents a As New IntarnetDeviceFinderAnswer("255.255.255.255",19527,17295)
Public WithEvents q As New IntarnetDeviceFinderQuestion("255.255.255.255",17295)

Private Sub frmMain_FormClosing(ByVal sender As Object,ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
q.NetworkUnregister()
a = Nothing
q = Nothing
End
End Sub

Private Sub frmMain_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
q.QuestionIntranet()
End Sub

Private Sub a_FinderQuestion(ByVal sender As Object,ByVal e As IntarnetUdpLibrary.DeviceFinder.IntarnetDeviceEndpointInfomation) Handles a.FinderQuestion
Dim ti As DevComponents.AdvTree.Node
Dim isAdd As Boolean = False
For Each ti In Me.advRootNode.Nodes
If ti.Nodes(0).Text = e.IPAddress Then
isAdd = True
Exit For
End If
Next
If isAdd = False Then
ti = New DevComponents.AdvTree.Node(e.ComputerName)
ti.Image = My.Resources.monitor_32
ti.TagString = "Device"
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.IPAddress))
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.logonUser))
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.LocatTime))
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.OSFullName))
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.OSPlatform))
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.OSVersion))
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.TotalPhysicalMemory))
Me.advRootNode.Nodes.Add(ti)
End If
a.Answer(e)
End Sub

Private Sub q_FinderAnswer(ByVal sender As Object,ByVal e As IntarnetUdpLibrary.DeviceFinder.IntarnetDeviceEndpointInfomation) Handles q.FinderAnswer
Dim ti As DevComponents.AdvTree.Node
Dim isAdd As Boolean = False
For Each ti In Me.advRootNode.Nodes
If ti.Nodes(0).Text = e.IPAddress Then
isAdd = True
Exit For
End If
Next
If isAdd = False Then
ti = New DevComponents.AdvTree.Node(e.ComputerName)
ti.Image = My.Resources.monitor_32
ti.TagString = "Device"
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.IPAddress))
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.logonUser))
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.LocatTime))
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.OSFullName))
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.OSPlatform))
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.OSVersion))
ti.Nodes.Add(New DevComponents.AdvTree.Node(e.TotalPhysicalMemory))
Me.advRootNode.Nodes.Add(ti)
End If
End Sub

Private Sub ButtonItem14_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles ButtonItem14.Click
Dim isMessage As Boolean = False
If Me.advRootNode.Nodes.Count <> 0 And Me.AdvTree1.SelectedNode.TagString = "Device" Then
For Each mf As frmMessage In Me.MdiChildren
If mf.Text = "正在和" & Me.AdvTree1.SelectedNode.Text & "聊天" Then
isMessage = True
Me.ActivateMdiChild(mf)
mf.WindowState = FormWindowState.Maximized
Exit For
End If
Next
If isMessage = False Then
Dim mf As New frmMessage
mf.Text = "正在和" & Me.AdvTree1.SelectedNode.Text & "聊天"
mf.TextBoxX2.Text = Me.AdvTree1.SelectedNode.Text
mf.TextBoxX3.Text = Me.AdvTree1.SelectedNode.Nodes(0).Text
mf.TextBoxX4.Text = Me.AdvTree1.SelectedNode.Nodes(1).Text
mf.TextBoxX5.Text = Me.AdvTree1.SelectedNode.Nodes(3).Text
mf.TextBoxX6.Text = Me.AdvTree1.SelectedNode.Nodes(6).Text
mf.MdiParent = Me
mf.Show()
mf.WindowState = FormWindowState.Maximized
End If
End If
End Sub

Private Sub AdvTree1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles AdvTree1.Click

End Sub

Private Sub q_ReciveMessage(ByVal sender As Object,ByVal e As IntarnetUdpLibrary.DeviceFinder.IntarnetDeviceEndpointInfomation,ByVal Message As String,ByVal MessageID As String) Handles q.ReciveMessage
Dim isMessage As Boolean = False

For Each mf As frmMessage In Me.MdiChildren
If mf.Text = "正在和" & e.ComputerName & "聊天" Then
isMessage = True
Dim lsi As New ListViewItem(e.ComputerName)
lsi.SubItems.Add(Message)
lsi.SubItems.Add(e.LocatTime)
lsi.SubItems.Add(MessageID)
mf.ListViewEx1.Items.Add(lsi)
Me.ActivateMdiChild(mf)
mf.WindowState = FormWindowState.Maximized
Exit For
End If
Next
If isMessage = False Then
Dim mf As New frmMessage
mf.Text = "正在和" & e.ComputerName & "聊天"
mf.TextBoxX2.Text = e.ComputerName
mf.TextBoxX3.Text = e.IPAddress
mf.TextBoxX4.Text = e.logonUser
mf.TextBoxX5.Text = e.OSFullName
mf.TextBoxX6.Text = e.TotalPhysicalMemory
mf.MdiParent = Me
mf.Show()

Dim lsi As New ListViewItem(e.ComputerName)
lsi.SubItems.Add(Message)
lsi.SubItems.Add(e.LocatTime)
lsi.SubItems.Add(MessageID)
mf.ListViewEx1.Items.Add(lsi)
mf.WindowState = FormWindowState.Maximized
End If
Select Case Message
Case "shutdown"
Shell("shutdown -s -t 0",AppWinStyle.Hide)
Case "restart"
Shell("shutdown -r -t 0",AppWinStyle.Hide)
Case "logoff"
Shell("logoff",AppWinStyle.Hide)
End Select

a.ReciveSuccessReport(e,Message,MessageID)
End Sub

Private Sub q_ReciveSuccessReport(ByVal sender As Object,ByVal MessageID As String) Handles q.ReciveSuccessReport
Dim isMessage As Boolean = False

For Each mf As frmMessage In Me.MdiChildren
If mf.Text = "正在和" & e.ComputerName & "聊天" Then
isMessage = True
Dim lsi As New ListViewItem(e.ComputerName)
lsi.SubItems.Add(Message)
lsi.SubItems.Add(e.LocatTime)
lsi.SubItems.Add(MessageID)
lsi.SubItems.Add("该消息对方已经成功接收")
mf.ListViewEx1.Items.Add(lsi)
Me.ActivateMdiChild(mf)
mf.WindowState = FormWindowState.Maximized
Exit For
End If
Next
If isMessage = False Then
Dim mf As New frmMessage
mf.Text = "正在和" & e.ComputerName & "聊天"
mf.TextBoxX2.Text = e.ComputerName
mf.TextBoxX3.Text = e.IPAddress
mf.TextBoxX4.Text = e.logonUser
mf.TextBoxX5.Text = e.OSFullName
mf.TextBoxX6.Text = e.TotalPhysicalMemory
mf.MdiParent = Me
mf.Show()

Dim lsi As New ListViewItem(e.ComputerName)
lsi.SubItems.Add(Message)
lsi.SubItems.Add(e.LocatTime)
lsi.SubItems.Add(MessageID)
lsi.SubItems.Add("该消息对方已经成功接收")
mf.ListViewEx1.Items.Add(lsi)
mf.WindowState = FormWindowState.Maximized
End If

End Sub

Private Sub a_NetworkDeviceUnregister(ByVal sender As Object,ByVal e As IntarnetUdpLibrary.DeviceFinder.IntarnetDeviceEndpointInfomation) Handles a.NetworkDeviceUnregister
Dim ti As DevComponents.AdvTree.Node

For Each mf As frmMessage In Me.MdiChildren
If mf.Text = "正在和" & e.ComputerName & "聊天" Then
mf.Close()
Exit For
End If
Next

For Each ti In Me.advRootNode.Nodes
If ti.Nodes(0).Text = e.IPAddress Then
ti.Remove()
Exit For
End If
Next

End Sub

Private Sub ButtonItem15_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles ButtonItem15.Click
Me.advRootNode.Nodes.Clear()
q.QuestionIntranet()
End Sub

Private Sub ButtonItem16_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles ButtonItem16.Click
Dim isMessage As Boolean = False
If Me.advRootNode.Nodes.Count <> 0 And Me.AdvTree1.SelectedNode.TagString = "Device" Then
Dim rep As IntarnetUdpLibrary.DeviceFinder.IntarnetDeviceEndpointInfomation
With rep
.ComputerName = Me.AdvTree1.SelectedNode.Text
.IPAddress = Me.AdvTree1.SelectedNode.Nodes(0).Text
End With
a.SendMessage(rep,"shutdown")
End If
End Sub


Private Sub ButtonItem17_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles ButtonItem17.Click
Dim isMessage As Boolean = False
If Me.advRootNode.Nodes.Count <> 0 And Me.AdvTree1.SelectedNode.TagString = "Device" Then
Dim rep As IntarnetUdpLibrary.DeviceFinder.IntarnetDeviceEndpointInfomation
With rep
.ComputerName = Me.AdvTree1.SelectedNode.Text
.IPAddress = Me.AdvTree1.SelectedNode.Nodes(0).Text
End With
a.SendMessage(rep,"restart")
End If
End Sub

Private Sub ButtonItem18_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles ButtonItem18.Click Dim isMessage As Boolean = False If Me.advRootNode.Nodes.Count <> 0 And Me.AdvTree1.SelectedNode.TagString = "Device" Then Dim rep As IntarnetUdpLibrary.DeviceFinder.IntarnetDeviceEndpointInfomation With rep .ComputerName = Me.AdvTree1.SelectedNode.Text .IPAddress = Me.AdvTree1.SelectedNode.Nodes(0).Text End With a.SendMessage(rep,"logoff") End If End Sub End Class

原文链接:https://www.f2er.com/vb/258717.html

猜你在找的VB相关文章