-
VB.NET实现DirectPlay(3)Find HOSTs
关键字: DirectPlay DPlay VB DirectX .net 网络 游戏 作者:董含君
转贴请注明来自 http://a11s.cnblogs.com
很像Server-Client的东西 Host-Client
利用刚才说的HOST建立方法建立HOST
稍微修改一下,毕竟不能每次都new一个Guid的.所以以后不出意外都用这个
Guid("358E1C8D-DB4A-4867-B2C3-DEF3F5046B17")
端口 2603
或许可以做成一个HOST类,呵呵,那样的话就是结构问题了,有点超出我的计划.我强调的是实现,至于如何做一个好的结构以后再说.
一开始走了不少弯路,而且有N次没有响应….
代码经过再三压缩,终于控制再100行左右了(自动生成的除外).代码多了容易让人误会的,比如微软的那个.
HOST一个需要7行.
说一下FindHOST的大体步骤
1 首先自己也是一个Peer啦,所以new一个就可,别的不要
2 appdescription 主要就是那个guid,必须一样(不一样我也不知道会怎样)
3 LocalAddress 本地地址,主要就是设置一下本地端口 比如2555
4 HOSTAddress 服务器地址,主要是协议,当然,多一些信息也可,具体看代码
5 添加一个事件,MS用的是addhandler 我个人习惯用 withevents(个人VB6的遗留问题..)用于响应HOST的回复
6 Peer.FindHOST(@#$%^$%^&*(&…..) 然后就等着信息吧.本来很简单的事情被弄得复杂了.整理完了之后发现真的很简单.具体的就不多说了,100行不到,自己看吧
注意1,find的时候最好用none,否则有可能导致假死或者错误.或者要求输入IP..
注意2,HOST的时候,如果你不发送本机,那么本机将会搜索不到,但是别的机器可以
Imports Microsoft.DirectX.DirectPlay
Public Class Form2
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.CheckBox1 = New System.Windows.Forms.CheckBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(0, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(88, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "HOST"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(96, 16)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(80, 32)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Left"
'
'CheckBox1
'
Me.CheckBox1.Checked = True
Me.CheckBox1.CheckState = System.Windows.Forms.CheckState.Checked
Me.CheckBox1.Location = New System.Drawing.Point(192, 24)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.Size = New System.Drawing.Size(96, 24)
Me.CheckBox1.TabIndex = 3
Me.CheckBox1.Text = "总是允许"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 61)
Me.Controls.Add(Me.CheckBox1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)
End Sub
#End Region
Dim WithEvents P As New peer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try '简化简化再简化,建立一个HOST就需要这么7行
Dim Addr As New Address("127.0.0.1", CInt(InputBox("端口", "Port", "2603")))
Addr.ServiceProvider = Address.ServiceProviderTcpIp
Dim appdesc As New ApplicationDescription
appdesc.Flags = SessionFlags.FullSigned
appdesc.GuidApplication = New Guid("358E1C8D-DB4A-4867-B2C3-DEF3F5046B17")
appdesc.SessionName = InputBox("起个名字", "Session Name", "session1")
P.Host(appdesc, Addr)
Me.Text = "HOST OK(" + appdesc.SessionName + ")"
Catch ex As Exception
P.Dispose()
Exit Sub
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
P.Dispose()
Me.Text = "Disposed HOST Peer"
End Sub
Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
P.Dispose()
End Sub
Private Sub P_FindHostQuery(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.FindHostQueryEventArgs) Handles P.FindHostQuery
If Not CheckBox1.Checked Then
If MsgBox("收到来自" + e.Message.AddressSender.KeyHostname + "的请求,接受?", MsgBoxStyle.YesNo) = MsgBoxResult.No Then e.RejectMessage = True
Else
e.RejectMessage = False
End If
End Sub
End Class
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
Imports Microsoft.DirectX.DirectPlay
Public Class Form1
Inherits System.Windows.Forms.Form
Dim WithEvents P As New Peer
Dim LocalAddr As New Address
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(104, 0)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(184, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Show HOST Window"
'
'ListBox1
'
Me.ListBox1.ItemHeight = 12
Me.ListBox1.Location = New System.Drawing.Point(8, 40)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(272, 208)
Me.ListBox1.TabIndex = 1
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(8, 8)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(80, 24)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Refresh"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'首先发一个请求,要求HOST回复
Dim appDesc As New ApplicationDescription
appdesc.GuidApplication = New Guid("358E1C8D-DB4A-4867-B2C3-DEF3F5046B17")
Dim HostAddr As New Address
HostAddr.ServiceProvider = Address.ServiceProviderTcpIp
'HostAddr.KeyPort = "2603"
'HostAddr.KeyHostname = "127.0.0.1"
Try
Me.Text = "Searching..."
ListBox1.Items.Clear()
P.FindHosts(appDesc, HostAddr, LocalAddr, Nothing, 1, 1000, 2000, FindHostsFlags.None)
Me.Text = "Search ok"
Catch ex As Exception
MsgBox("Failed!")
End Try
End Sub
Private Sub P_FindHostResponse(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.FindHostResponseEventArgs) Handles P.FindHostResponse
'收到HOST的回复
ListBox1.Items.Add("SessionName:" + e.Message.ApplicationDescription.SessionName)
'ListBox1.Items.Add(" 端口:" + e.Message.AddressSender.KeyPort)
ListBox1.Items.Add(" HOST:" + e.Message.AddressSender.KeyHostname)
ListBox1.Items.Add(" GuidInstance:" + e.Message.ApplicationDescription.GuidInstance.ToString)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
LocalAddr.KeyPort = 2555
LocalAddr.KeyHostname = "127.0.0.1"
LocalAddr.ServiceProvider = Address.ServiceProviderTcpIp
Catch ex As Exception
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form2
f.Show()
End Sub
End Class
转贴请注明来自 http://a11s.cnblogs.com
很像Server-Client的东西 Host-Client
利用刚才说的HOST建立方法建立HOST
稍微修改一下,毕竟不能每次都new一个Guid的.所以以后不出意外都用这个
Guid("358E1C8D-DB4A-4867-B2C3-DEF3F5046B17")
端口 2603
或许可以做成一个HOST类,呵呵,那样的话就是结构问题了,有点超出我的计划.我强调的是实现,至于如何做一个好的结构以后再说.
一开始走了不少弯路,而且有N次没有响应….
代码经过再三压缩,终于控制再100行左右了(自动生成的除外).代码多了容易让人误会的,比如微软的那个.
HOST一个需要7行.
说一下FindHOST的大体步骤
1 首先自己也是一个Peer啦,所以new一个就可,别的不要
2 appdescription 主要就是那个guid,必须一样(不一样我也不知道会怎样)
3 LocalAddress 本地地址,主要就是设置一下本地端口 比如2555
4 HOSTAddress 服务器地址,主要是协议,当然,多一些信息也可,具体看代码
5 添加一个事件,MS用的是addhandler 我个人习惯用 withevents(个人VB6的遗留问题..)用于响应HOST的回复
6 Peer.FindHOST(@#$%^$%^&*(&…..) 然后就等着信息吧.本来很简单的事情被弄得复杂了.整理完了之后发现真的很简单.具体的就不多说了,100行不到,自己看吧
注意1,find的时候最好用none,否则有可能导致假死或者错误.或者要求输入IP..
注意2,HOST的时候,如果你不发送本机,那么本机将会搜索不到,但是别的机器可以
Imports Microsoft.DirectX.DirectPlay
Public Class Form2
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.CheckBox1 = New System.Windows.Forms.CheckBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(0, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(88, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "HOST"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(96, 16)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(80, 32)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Left"
'
'CheckBox1
'
Me.CheckBox1.Checked = True
Me.CheckBox1.CheckState = System.Windows.Forms.CheckState.Checked
Me.CheckBox1.Location = New System.Drawing.Point(192, 24)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.Size = New System.Drawing.Size(96, 24)
Me.CheckBox1.TabIndex = 3
Me.CheckBox1.Text = "总是允许"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 61)
Me.Controls.Add(Me.CheckBox1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)
End Sub
#End Region
Dim WithEvents P As New peer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try '简化简化再简化,建立一个HOST就需要这么7行
Dim Addr As New Address("127.0.0.1", CInt(InputBox("端口", "Port", "2603")))
Addr.ServiceProvider = Address.ServiceProviderTcpIp
Dim appdesc As New ApplicationDescription
appdesc.Flags = SessionFlags.FullSigned
appdesc.GuidApplication = New Guid("358E1C8D-DB4A-4867-B2C3-DEF3F5046B17")
appdesc.SessionName = InputBox("起个名字", "Session Name", "session1")
P.Host(appdesc, Addr)
Me.Text = "HOST OK(" + appdesc.SessionName + ")"
Catch ex As Exception
P.Dispose()
Exit Sub
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
P.Dispose()
Me.Text = "Disposed HOST Peer"
End Sub
Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
P.Dispose()
End Sub
Private Sub P_FindHostQuery(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.FindHostQueryEventArgs) Handles P.FindHostQuery
If Not CheckBox1.Checked Then
If MsgBox("收到来自" + e.Message.AddressSender.KeyHostname + "的请求,接受?", MsgBoxStyle.YesNo) = MsgBoxResult.No Then e.RejectMessage = True
Else
e.RejectMessage = False
End If
End Sub
End Class
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
Imports Microsoft.DirectX.DirectPlay
Public Class Form1
Inherits System.Windows.Forms.Form
Dim WithEvents P As New Peer
Dim LocalAddr As New Address
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(104, 0)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(184, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Show HOST Window"
'
'ListBox1
'
Me.ListBox1.ItemHeight = 12
Me.ListBox1.Location = New System.Drawing.Point(8, 40)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(272, 208)
Me.ListBox1.TabIndex = 1
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(8, 8)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(80, 24)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Refresh"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'首先发一个请求,要求HOST回复
Dim appDesc As New ApplicationDescription
appdesc.GuidApplication = New Guid("358E1C8D-DB4A-4867-B2C3-DEF3F5046B17")
Dim HostAddr As New Address
HostAddr.ServiceProvider = Address.ServiceProviderTcpIp
'HostAddr.KeyPort = "2603"
'HostAddr.KeyHostname = "127.0.0.1"
Try
Me.Text = "Searching..."
ListBox1.Items.Clear()
P.FindHosts(appDesc, HostAddr, LocalAddr, Nothing, 1, 1000, 2000, FindHostsFlags.None)
Me.Text = "Search ok"
Catch ex As Exception
MsgBox("Failed!")
End Try
End Sub
Private Sub P_FindHostResponse(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.FindHostResponseEventArgs) Handles P.FindHostResponse
'收到HOST的回复
ListBox1.Items.Add("SessionName:" + e.Message.ApplicationDescription.SessionName)
'ListBox1.Items.Add(" 端口:" + e.Message.AddressSender.KeyPort)
ListBox1.Items.Add(" HOST:" + e.Message.AddressSender.KeyHostname)
ListBox1.Items.Add(" GuidInstance:" + e.Message.ApplicationDescription.GuidInstance.ToString)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
LocalAddr.KeyPort = 2555
LocalAddr.KeyHostname = "127.0.0.1"
LocalAddr.ServiceProvider = Address.ServiceProviderTcpIp
Catch ex As Exception
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form2
f.Show()
End Sub
End Class
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式