VB.NET实现DirectPlay(3)Find HOSTs
關鍵字: DirectPlay DPlay VB DirectX .net 網絡 游戲 作者:董含君
轉貼請注明來自 http://a11s.cnblogs.com
?
很像Server-Client的東西 Host-Client<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
利用剛才說的HOST建立方法建立HOST
稍微修改一下,畢竟不能每次都new一個Guid的.所以以后不出意外都用這個
Guid("358E<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />1C8D-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
?
?
posted on 2005-11-01 17:17 a11s.net 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/a11s/archive/2005/11/01/266508.html
總結
以上是生活随笔為你收集整理的VB.NET实现DirectPlay(3)Find HOSTs的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三星 Galaxy Watch 未来可内
- 下一篇: 三星 Galaxy S23 / Ultr