a11s.Net

年纪大了,脑子不好用了,需要记录写东西了

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

关键字: 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

 

 

posted on 2005-11-01 17:17  a11s.net  阅读(1126)  评论(0编辑  收藏  举报