-
VB.NET实现DirectPlay(4)chat
关键字: DirectPlay DPlay VB DirectX .net 网络 游戏 作者:董含君
转贴请注明来自 http://a11s.cnblogs.com
身体状况不是很好.感冒好几天了.现在嗓子还不能说话.昨天本来想发chat的,结果cnblogs居然出奇的卡.唉….
我还不知道怎么向blog上传rar.不然我也不用这么费事贴代码了.或者谁有其他的好办发来介绍一下.我现在再QQ群: 589626 C#游戏开发(-_- 好像我一直VB…) 这里应该能找到我们
不说废话了.先看截图
虽然努力压缩了,但是还是少不了200行.而且由于释放的代码省略了不少.导致多次HOST会出现问题.还有就是search host之后,一次只能接受一个,其他的点”否”.虽然guid是一样的,但是我没有进行筛选.响应的筛选代码不难,无非是用一个hashtable之类.
大体步骤是这样的.扫描完hosts之后,等待host的回复.host会给出他的applicationdescription
以及address信息当然,连通你的address也一起给返回了.从这里我们可以得到sessionname ip port等很多信息,但是,我们完全不用关心….. 只要知道sessionname以及它反馈的appdesc address对象 就够了
1 hostqueryresponse的时候建立列表,或者询问是否接受.
Peer.connect(参数列表) 还记得我们搜索时候的那个长长的列表吗?就是那个顺序,只不过我们搜索的时候填写的appdesc 以及address信息不完整罢了.这次是将host的appdesc以及address给反馈回来.也就是说,根据这两个对象可以确定唯一的host.
ClientPeer.Connect(e.Message.ApplicationDescription, e.Message.AddressSender, ClientAddr, Nothing, ConnectFlags.Sync)
2 连接成功会有一个ConnectComplete事件,你可以提示用户连接已经建立 ^-^
3 同样的,也会发生SessionTerminated事件,这些响应的事件可以从列表得到.自己研究吧,是不是感觉跟VB6的WINSOCK Control差不多了.
4 发送信息.这个发送的是一个networkpacket对象.new一个看看无非是read write 方法.我们把要发送的数据(或者字符串)write进去.然后peer.sendto (playerid.allplayer,,,,,)既可.当然也可以发给某一个用户,只不过这个playerid如何取得目前我还没搞定.因为我还不会啊….本文是学习笔记…我要是一开始什么都会就不这么写了…
Imports Microsoft.DirectX.DirectPlay
Public Class Form1
Inherits System.Windows.Forms.Form
Dim g As Guid = New Guid("590E16DD-538F-40fb-A7DF-9654EA6BE71E")
'因为自己既要做HOST又要做Client所以干脆都声明一下免得看的混乱
'都还没有实例化,到时候根据是否HOST再来定义
Dim IsHost As Boolean = False
Dim IsConnected As Boolean = False
Dim WithEvents HostPeer As Peer
Dim WithEvents ClientPeer As Peer
Dim HostAddr As Address
Dim ClientAddr As Address
Dim HostDesc As ApplicationDescription
Dim ClientDesc As ApplicationDescription
#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 Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents Button5 As System.Windows.Forms.Button
Friend WithEvents Button6 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.Button5 = New System.Windows.Forms.Button
Me.Button6 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(120, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "HOST"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(160, 8)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(120, 32)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Left"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(8, 56)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(120, 32)
Me.Button3.TabIndex = 3
Me.Button3.Text = "Search"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(160, 56)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(120, 32)
Me.Button4.TabIndex = 4
Me.Button4.Text = "Left Session"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(8, 168)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(184, 21)
Me.TextBox2.TabIndex = 5
Me.TextBox2.Text = "TextBox2"
'
'ListBox1
'
Me.ListBox1.ItemHeight = 12
Me.ListBox1.Location = New System.Drawing.Point(8, 96)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(272, 64)
Me.ListBox1.TabIndex = 6
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(208, 168)
Me.Button5.Name = "Button5"
Me.Button5.Size = New System.Drawing.Size(72, 24)
Me.Button5.TabIndex = 7
Me.Button5.Text = "send"
'
'Button6
'
Me.Button6.Location = New System.Drawing.Point(8, 216)
Me.Button6.Name = "Button6"
Me.Button6.Size = New System.Drawing.Size(96, 24)
Me.Button6.TabIndex = 8
Me.Button6.Text = "New Me"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button6)
Me.Controls.Add(Me.Button5)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'自己HOST一下
If IsHost Then
MsgBox("你看看标题栏,已经HOST了,别点了")
Exit Sub
End If
Try
'如果参数胡乱填写很可能失败的
HostAddr = New Address
HostAddr.KeyHostname = InputBox("HOST Name:", "IP或者域名", "127.0.0.1")
HostAddr.KeyPort = InputBox("PORT:", "端口", "2603")
HostAddr.ServiceProvider = Address.ServiceProviderTcpIp
HostDesc = New ApplicationDescription
HostDesc.SessionName = InputBox("Session Name:", "起个名字", "Session1")
HostDesc.GuidApplication = g
HostDesc.Flags = SessionFlags.NoDpnServer
HostPeer = New Peer
HostPeer.Host(HostDesc, HostAddr)
Me.Text = HostDesc.SessionName
IsHost = True
Catch ex As Exception
MsgBox("失败:" + ex.Message)
Try
HostPeer.Dispose()
'失败就Over掉它
Me.Text = "No Host"
Catch exx As Exception
End Try
IsHost = False
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Not IsHost Then Exit Sub
Try
HostPeer.Dispose()
'失败就Over掉它
Me.Text = "No Host"
IsHost = False
Catch exx As Exception
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'找啊找啊找Session,找到一个好Session,敬个礼啊握握手,你是我的好Session
If IsHost Then
MsgBox("你还是再new一个窗体吧,这个已经用来host了")
Exit Sub
End If
HostAddr = New Address
HostAddr.ServiceProvider = HostAddr.ServiceProviderTcpIp
HostDesc.GuidApplication = g
'HostDesc.Flags = SessionFlags.NoDpnServer
ClientAddr = New Address
ClientAddr.KeyHostname = InputBox("本地IP", "HostName", "127.0.0.1")
ClientAddr.KeyPort = InputBox("本地端口", "端口", "3613")
ClientAddr.ServiceProvider = Address.ServiceProviderTcpIp
ClientPeer = New Peer
Me.Text = "Searching..."
ClientPeer.FindHosts(HostDesc, HostAddr, ClientAddr, Nothing, 1, 10, 2000, FindHostsFlags.None)
Me.Text = "Search OK"
'OK 期待回复吧
End Sub
Private Sub ClientPeer_FindHostResponse(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.FindHostResponseEventArgs) Handles ClientPeer.FindHostResponse
'如果已经连接了,那么这个就没意义了
If IsConnected Then Exit Sub
'假设这个时候 clientPeer还没有被你释放,其实应该try一下的... -_-b
If MsgBox("找到一个连接,Session='" + e.Message.ApplicationDescription.SessionName + "' 是否连接?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Me.Text = "连接中..."
ClientPeer.Connect(e.Message.ApplicationDescription, e.Message.AddressSender, ClientAddr, Nothing, ConnectFlags.Sync)
End If
End Sub
Private Sub ClientPeer_ConnectComplete(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.ConnectCompleteEventArgs) Handles ClientPeer.ConnectComplete
Me.Text = "连接成功"
IsConnected = True
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
'发信息啊发信息
'没有连接 或者 自己不是主机
If (Not IsConnected) And (Not IsHost) Then
MsgBox("没有连接啊")
Exit Sub
End If
Dim pak As New NetworkPacket
pak.Write(TextBox2.Text)
If IsHost Then
HostPeer.SendTo(PlayerID.AllPlayers, pak, 1000, SendFlags.Guaranteed)
Else
ClientPeer.SendTo(PlayerID.AllPlayers, pak, 1000, SendFlags.Guaranteed)
End If
End Sub
Private Sub ClientPeer_Receive(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.ReceiveEventArgs) Handles ClientPeer.Receive
ListBox1.Items.Add(e.Message.ReceiveData.ReadString)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If IsHost Then Exit Sub
If ClientPeer Is Nothing Then Exit Sub
IsConnected = False
ClientPeer.Dispose()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim f As New Form1
f.Show()
End Sub
Private Sub HostPeer_Receive(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.ReceiveEventArgs) Handles HostPeer.Receive
'如果做HOST 自然是HOSTPEER 受到信息了
ListBox1.Items.Add(e.Message.ReceiveData.ReadString)
End Sub
Private Sub ClientPeer_SessionTerminated(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.SessionTerminatedEventArgs) Handles ClientPeer.SessionTerminated
Try
ClientPeer.Dispose()
MsgBox("SessionTerminated")
Catch ex As Exception
End Try
End Sub
Private Sub ClientPeer_IndicatedConnectAborted(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.IndicatedConnectAbortedEventArgs) Handles ClientPeer.IndicatedConnectAborted
Try
ClientPeer.Dispose()
MsgBox("ConnectAborted")
Catch ex As Exception
End Try
End Sub
End Class
转贴请注明来自 http://a11s.cnblogs.com
身体状况不是很好.感冒好几天了.现在嗓子还不能说话.昨天本来想发chat的,结果cnblogs居然出奇的卡.唉….
我还不知道怎么向blog上传rar.不然我也不用这么费事贴代码了.或者谁有其他的好办发来介绍一下.我现在再QQ群: 589626 C#游戏开发(-_- 好像我一直VB…) 这里应该能找到我们
不说废话了.先看截图
虽然努力压缩了,但是还是少不了200行.而且由于释放的代码省略了不少.导致多次HOST会出现问题.还有就是search host之后,一次只能接受一个,其他的点”否”.虽然guid是一样的,但是我没有进行筛选.响应的筛选代码不难,无非是用一个hashtable之类.
大体步骤是这样的.扫描完hosts之后,等待host的回复.host会给出他的applicationdescription
以及address信息当然,连通你的address也一起给返回了.从这里我们可以得到sessionname ip port等很多信息,但是,我们完全不用关心….. 只要知道sessionname以及它反馈的appdesc address对象 就够了
1 hostqueryresponse的时候建立列表,或者询问是否接受.
Peer.connect(参数列表) 还记得我们搜索时候的那个长长的列表吗?就是那个顺序,只不过我们搜索的时候填写的appdesc 以及address信息不完整罢了.这次是将host的appdesc以及address给反馈回来.也就是说,根据这两个对象可以确定唯一的host.
ClientPeer.Connect(e.Message.ApplicationDescription, e.Message.AddressSender, ClientAddr, Nothing, ConnectFlags.Sync)
2 连接成功会有一个ConnectComplete事件,你可以提示用户连接已经建立 ^-^
3 同样的,也会发生SessionTerminated事件,这些响应的事件可以从列表得到.自己研究吧,是不是感觉跟VB6的WINSOCK Control差不多了.
4 发送信息.这个发送的是一个networkpacket对象.new一个看看无非是read write 方法.我们把要发送的数据(或者字符串)write进去.然后peer.sendto (playerid.allplayer,,,,,)既可.当然也可以发给某一个用户,只不过这个playerid如何取得目前我还没搞定.因为我还不会啊….本文是学习笔记…我要是一开始什么都会就不这么写了…
5 利用receive事件得到这个packet,然后e.Message.ReceiveData.ReadString直接得到字符串
贴代码的时间到了…Imports Microsoft.DirectX.DirectPlay
Public Class Form1
Inherits System.Windows.Forms.Form
Dim g As Guid = New Guid("590E16DD-538F-40fb-A7DF-9654EA6BE71E")
'因为自己既要做HOST又要做Client所以干脆都声明一下免得看的混乱
'都还没有实例化,到时候根据是否HOST再来定义
Dim IsHost As Boolean = False
Dim IsConnected As Boolean = False
Dim WithEvents HostPeer As Peer
Dim WithEvents ClientPeer As Peer
Dim HostAddr As Address
Dim ClientAddr As Address
Dim HostDesc As ApplicationDescription
Dim ClientDesc As ApplicationDescription
#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 Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents Button5 As System.Windows.Forms.Button
Friend WithEvents Button6 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.Button5 = New System.Windows.Forms.Button
Me.Button6 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(120, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "HOST"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(160, 8)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(120, 32)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Left"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(8, 56)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(120, 32)
Me.Button3.TabIndex = 3
Me.Button3.Text = "Search"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(160, 56)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(120, 32)
Me.Button4.TabIndex = 4
Me.Button4.Text = "Left Session"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(8, 168)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(184, 21)
Me.TextBox2.TabIndex = 5
Me.TextBox2.Text = "TextBox2"
'
'ListBox1
'
Me.ListBox1.ItemHeight = 12
Me.ListBox1.Location = New System.Drawing.Point(8, 96)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(272, 64)
Me.ListBox1.TabIndex = 6
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(208, 168)
Me.Button5.Name = "Button5"
Me.Button5.Size = New System.Drawing.Size(72, 24)
Me.Button5.TabIndex = 7
Me.Button5.Text = "send"
'
'Button6
'
Me.Button6.Location = New System.Drawing.Point(8, 216)
Me.Button6.Name = "Button6"
Me.Button6.Size = New System.Drawing.Size(96, 24)
Me.Button6.TabIndex = 8
Me.Button6.Text = "New Me"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button6)
Me.Controls.Add(Me.Button5)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'自己HOST一下
If IsHost Then
MsgBox("你看看标题栏,已经HOST了,别点了")
Exit Sub
End If
Try
'如果参数胡乱填写很可能失败的
HostAddr = New Address
HostAddr.KeyHostname = InputBox("HOST Name:", "IP或者域名", "127.0.0.1")
HostAddr.KeyPort = InputBox("PORT:", "端口", "2603")
HostAddr.ServiceProvider = Address.ServiceProviderTcpIp
HostDesc = New ApplicationDescription
HostDesc.SessionName = InputBox("Session Name:", "起个名字", "Session1")
HostDesc.GuidApplication = g
HostDesc.Flags = SessionFlags.NoDpnServer
HostPeer = New Peer
HostPeer.Host(HostDesc, HostAddr)
Me.Text = HostDesc.SessionName
IsHost = True
Catch ex As Exception
MsgBox("失败:" + ex.Message)
Try
HostPeer.Dispose()
'失败就Over掉它
Me.Text = "No Host"
Catch exx As Exception
End Try
IsHost = False
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Not IsHost Then Exit Sub
Try
HostPeer.Dispose()
'失败就Over掉它
Me.Text = "No Host"
IsHost = False
Catch exx As Exception
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'找啊找啊找Session,找到一个好Session,敬个礼啊握握手,你是我的好Session
If IsHost Then
MsgBox("你还是再new一个窗体吧,这个已经用来host了")
Exit Sub
End If
HostAddr = New Address
HostAddr.ServiceProvider = HostAddr.ServiceProviderTcpIp
HostDesc.GuidApplication = g
'HostDesc.Flags = SessionFlags.NoDpnServer
ClientAddr = New Address
ClientAddr.KeyHostname = InputBox("本地IP", "HostName", "127.0.0.1")
ClientAddr.KeyPort = InputBox("本地端口", "端口", "3613")
ClientAddr.ServiceProvider = Address.ServiceProviderTcpIp
ClientPeer = New Peer
Me.Text = "Searching..."
ClientPeer.FindHosts(HostDesc, HostAddr, ClientAddr, Nothing, 1, 10, 2000, FindHostsFlags.None)
Me.Text = "Search OK"
'OK 期待回复吧
End Sub
Private Sub ClientPeer_FindHostResponse(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.FindHostResponseEventArgs) Handles ClientPeer.FindHostResponse
'如果已经连接了,那么这个就没意义了
If IsConnected Then Exit Sub
'假设这个时候 clientPeer还没有被你释放,其实应该try一下的... -_-b
If MsgBox("找到一个连接,Session='" + e.Message.ApplicationDescription.SessionName + "' 是否连接?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Me.Text = "连接中..."
ClientPeer.Connect(e.Message.ApplicationDescription, e.Message.AddressSender, ClientAddr, Nothing, ConnectFlags.Sync)
End If
End Sub
Private Sub ClientPeer_ConnectComplete(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.ConnectCompleteEventArgs) Handles ClientPeer.ConnectComplete
Me.Text = "连接成功"
IsConnected = True
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
'发信息啊发信息
'没有连接 或者 自己不是主机
If (Not IsConnected) And (Not IsHost) Then
MsgBox("没有连接啊")
Exit Sub
End If
Dim pak As New NetworkPacket
pak.Write(TextBox2.Text)
If IsHost Then
HostPeer.SendTo(PlayerID.AllPlayers, pak, 1000, SendFlags.Guaranteed)
Else
ClientPeer.SendTo(PlayerID.AllPlayers, pak, 1000, SendFlags.Guaranteed)
End If
End Sub
Private Sub ClientPeer_Receive(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.ReceiveEventArgs) Handles ClientPeer.Receive
ListBox1.Items.Add(e.Message.ReceiveData.ReadString)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If IsHost Then Exit Sub
If ClientPeer Is Nothing Then Exit Sub
IsConnected = False
ClientPeer.Dispose()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim f As New Form1
f.Show()
End Sub
Private Sub HostPeer_Receive(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.ReceiveEventArgs) Handles HostPeer.Receive
'如果做HOST 自然是HOSTPEER 受到信息了
ListBox1.Items.Add(e.Message.ReceiveData.ReadString)
End Sub
Private Sub ClientPeer_SessionTerminated(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.SessionTerminatedEventArgs) Handles ClientPeer.SessionTerminated
Try
ClientPeer.Dispose()
MsgBox("SessionTerminated")
Catch ex As Exception
End Try
End Sub
Private Sub ClientPeer_IndicatedConnectAborted(ByVal sender As Object, ByVal e As Microsoft.DirectX.DirectPlay.IndicatedConnectAbortedEventArgs) Handles ClientPeer.IndicatedConnectAborted
Try
ClientPeer.Dispose()
MsgBox("ConnectAborted")
Catch ex As Exception
End Try
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.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式