1 这个还是peer. 首先new一个GUID.用来表示程序.书上说这个Guid不能随便改变.所以你生成一个guid之后建议保存下来.生成guid的方法很简单学习笔记,VB.NET实现DirectPlay (1) HOST学习笔记,VB.NET实现DirectPlay (1) HOST
dim guidString as string= guid.newguid().tostring
刚才的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:
|
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) HOSTcomponents.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