×

学习笔记,VB.NET实现DirectPlay (1) HOST

Kalet Kalet 发表于2009-03-20 12:00:13 浏览317 评论0

抢沙发发表评论

这次需要上一篇文章的设备列表.

这次是建立一个HOST,虽然目前还不能实质的做什么,一步一步来嘛…
1 这个还是peer. 首先new一个GUID.用来表示程序.书上说这个Guid不能随便改变.所以你生成一个guid之后建议保存下来.生成guid的方法很简单学习笔记,VB.NET实现DirectPlay (1) HOST学习笔记,VB.NET实现DirectPlay (1) HOST
dim guidString as string= guid.newguid().tostring

用的时候直接 dim g as new guid(guidstring) 就可以搞定

2 建立应用程序描述 就是applicationDescription 其他DX组建也有类似信息
刚才的Guid就是其中一部分.还有一个SessionFlag程序描述需要指定.无非以下值,不用翻译吧…






























MEMBER


DESCRIPTION


GuidApplication


Uniquely identify your application.


GuidInstance


A unique identifier for your session, generated by DirectPlay. This is to identify separate instances of your application running simultaneously.


MaxPlayers


The maximum number of users allowed in a given session. Setting this value to zero (the default) will allow an unlimited number of players.


CurrentPlayers


The number of users currently in the session.


Flags


Used to define the session behavior, may be one or more of the following flags:



  • ClientServer
  • FastSigned
  • FullSigned
  • MigrateHost
  • NoDpnServer
  • NoEnumerations
  • RequirePassword

SessionName


User-defined name of the session.


Password


The password needed to join the session. This value must be null unless the RequirePassword flag is set.


3 IP
new 一个 address就可以,比如        Dim mAddr As New Address("127.0.0.1", 2552)



4 Peer 直接new一个就OK了
有了address 有了 appdesc 然后直接 peer.host (address,appdesc) 一切OK,还是比较简单的.MS的例子比较复杂.经过提纯,以下部分应该有用.虽然不够健壮,但是,为了说明步骤方便理解,所以不需要他多代码影响思维.

Imports Microsoft.DirectX.DirectPlay


Public Class Form1


    Inherits System.Windows.Forms.Form


    Dim WithEvents p As New Peer '新的peer



#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

学习笔记,VB.NET实现DirectPlay (1) HOST

                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


    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()


        Me.Button1 = New System.Windows.Forms.Button


        Me.SuspendLayout()


        '


        'Button1


        '


        Me.Button1.Location = New System.Drawing.Point(32, 16)


        Me.Button1.Name = "Button1"


        Me.Button1.Size = New System.Drawing.Size(72, 32)


        Me.Button1.TabIndex = 0


        Me.Button1.Text = "HOST"


        '


        'Form1


        '


        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)


        Me.ClientSize = New System.Drawing.Size(288, 197)


        Me.Controls.Add(Me.Button1)


        Me.Name = "Form1"


        Me.Text = "Form1"


        Me.ResumeLayout(False)



    End Sub



#End Region


    Dim g As Guid


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        g = Guid.NewGuid '生成一个新的GUID,用作标识application


        Me.Text = g.ToString '这个显示在窗体标题栏


        Dim mAddr As New Address("127.0.0.1", 2552)


        mAddr.ServiceProvider = Address.ServiceProviderTcpIp


        If IsServiceProviderValid(mAddr.ServiceProvider) = False Then


            '天,还真的出问题了?


            MsgBox("创建连接的时候失败了,网络设备不能用")


            Exit Sub


        End If


        Dim appDesc As New ApplicationDescription


        appDesc.GuidApplication = g '用上刚才随机生成的Guid了,当然这里仅仅是演示,要是每次都随机生成就不用连接了


        appDesc.Flags = SessionFlags.NoDpnServer Or SessionFlags.RequirePassword


        '以下都是其他属性的演示


        appDesc.SessionName = "test"


        appDesc.MaxPlayers = 10


        appDesc.Password = "123" '这里没有加密,都是明文传输,记得写上SessionFlags.RequirePassword 否则会出问题



        p.Host(appDesc, mAddr)




        MsgBox("OK", MsgBoxStyle.OKOnly, "Test")


    End Sub


    Private Function IsServiceProviderValid(ByVal provider As Guid) As Boolean


        '这个就是上一篇关于枚举设备的,看看这指定的设备是否能用


        '虽然一般没有问题,但是MS的例子都要检查一下,所以,还是稍微重视一下吧


        ' Ask DirectPlay for the service provider list


        Dim SPInfoArray As ServiceProviderInformation() = p.GetServiceProviders(True)



        ' For each service provider in the returned list...


        Dim info As ServiceProviderInformation


        For Each info In SPInfoArray


            ' Compare the current provider against the passed provider


            If info.Guid.Equals(provider) Then


                Return True


            End If


        Next info


        ' Not found


        Return False


    End Function 'IsServiceProviderValid



    Protected Overrides Sub Finalize()


        MyBase.Finalize()


        p.Dispose()


    End Sub

学习笔记,VB.NET实现DirectPlay (1) HOST
End Class


群贤毕至

访客