2 ' Copyright (c) 2010 Apple Inc. All rights reserved.
4 ' Licensed under the Apache License, Version 2.0 (the "License");
5 ' you may not use this file except in compliance with the License.
6 ' You may obtain a copy of the License at
8 ' http://www.apache.org/licenses/LICENSE-2.0
10 ' Unless required by applicable law or agreed to in writing, software
11 ' distributed under the License is distributed on an "AS IS" BASIS,
12 ' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ' See the License for the specific language governing permissions and
14 ' limitations under the License.
18 Imports System.Net.Sockets
22 Public Class SimpleChat
24 ' Associate Bonjour events with event handlers
26 Public WithEvents MyEventManager As New Bonjour.DNSSDEventManager
27 Private m_service As New Bonjour.DNSSDService
28 Private m_registrar As Bonjour.DNSSDService
29 Private m_browser As Bonjour.DNSSDService
30 Private m_resolver As Bonjour.DNSSDService
31 Private m_socket As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
32 Private m_port As Integer
33 Private m_buffer(1024 * 32) As Byte
34 Private m_async As IAsyncResult
35 Public Delegate Sub SocketDelegate(ByVal msg As String)
36 Private m_socketDelegate As SocketDelegate
37 Private m_name As String
42 'This call is required by the Windows Form Designer.
45 Button1.Enabled = False
47 m_socketDelegate = New SocketDelegate(AddressOf MessageReceived)
49 Dim endPoint As New IPEndPoint(IPAddress.Any, 0)
50 m_socket.Bind(endPoint)
51 endPoint = m_socket.LocalEndPoint
52 m_port = endPoint.Port
54 Dim txtRecord As Bonjour.TXTRecord
55 m_async = m_socket.BeginReceive(m_buffer, 0, m_buffer.Length, SocketFlags.Partial, New AsyncCallback(AddressOf OnReceive), Me)
56 m_registrar = m_service.Register(0, 0, Environment.UserName, "_p2pchat._udp", vbNullString, vbNullString, m_port, txtRecord, MyEventManager)
60 ' Called when Bonjour core finished registering a service successfully
62 Public Sub MyEventManager_ServiceRegistered(ByVal registrar As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal name As String, ByVal regType As String, ByVal domain As String) Handles MyEventManager.ServiceRegistered
64 m_browser = m_service.Browse(0, 0, regType, vbNullString, MyEventManager)
68 ' Called when a service is found
70 Public Sub MyEventManager_ServiceFound(ByVal browser As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal ifIndex As UInteger, ByVal serviceName As String, ByVal regtype As String, ByVal domain As String) Handles MyEventManager.ServiceFound
71 If (serviceName <> m_name) Then
72 Dim peer As PeerData = New PeerData
73 peer.InterfaceIndex = ifIndex
74 peer.Name = serviceName
77 ComboBox1.Items.Add(peer)
78 ComboBox1.SelectedIndex = 0
83 ' Called when a service is lost
85 Public Sub MyEventManager_ServiceLost(ByVal browser As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal ifIndex As UInteger, ByVal serviceName As String, ByVal regtype As String, ByVal domain As String) Handles MyEventManager.ServiceLost
86 ComboBox1.Items.Remove(serviceName)
90 ' Called when a service is resolved
92 Public Sub MyEventManager_ServiceResolved(ByVal resolver As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal ifIndex As UInteger, ByVal fullname As String, ByVal hostname As String, ByVal port As UShort, ByVal record As Bonjour.TXTRecord) Handles MyEventManager.ServiceResolved
94 Dim peer As PeerData = ComboBox1.SelectedItem
96 m_resolver = m_service.QueryRecord(0, ifIndex, hostname, Bonjour.DNSSDRRType.kDNSSDType_A, Bonjour.DNSSDRRClass.kDNSSDClass_IN, MyEventManager)
99 Public Sub MyEventManager_QueryAnswered(ByVal resolver As Bonjour.DNSSDService, ByVal flags As Bonjour.DNSSDFlags, ByVal ifIndex As UInteger, ByVal fullName As String, ByVal rrtype As Bonjour.DNSSDRRType, ByVal rrclass As Bonjour.DNSSDRRClass, ByVal rdata As Object, ByVal ttl As UInteger) Handles MyEventManager.QueryRecordAnswered
101 Dim peer As PeerData = ComboBox1.SelectedItem
102 Dim bits As UInteger = BitConverter.ToUInt32(rdata, 0)
103 Dim address As IPAddress = New System.Net.IPAddress(bits)
104 peer.Address = address
107 Public Sub MyEventManager_OperationFailed(ByVal registrar As Bonjour.DNSSDService, ByVal errorCode As Bonjour.DNSSDError) Handles MyEventManager.OperationFailed
108 MessageBox.Show("Operation failed error code: " + errorCode)
111 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
112 Dim peer As PeerData = ComboBox1.SelectedItem
113 Dim message As String = m_name + ": " + TextBox2.Text
114 Dim bytes As Byte() = Encoding.UTF8.GetBytes(message)
115 Dim endPoint As IPEndPoint = New IPEndPoint(peer.Address, peer.Port)
116 m_socket.SendTo(bytes, 0, bytes.Length, 0, endPoint)
117 TextBox1.AppendText(TextBox2.Text + Environment.NewLine)
121 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
122 Dim peer As PeerData = ComboBox1.SelectedItem
123 m_resolver = m_service.Resolve(0, peer.InterfaceIndex, peer.Name, peer.Type, peer.Domain, MyEventManager)
125 Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
126 Dim peer As PeerData = ComboBox1.SelectedItem
127 If ((peer.Address IsNot Nothing) And TextBox2.Text.Length > 0) Then
128 Button1.Enabled = True
130 Button1.Enabled = False
133 Public Sub MessageReceived(ByVal msg As System.String)
134 TextBox1.AppendText(msg)
136 Private Sub OnReceive(ByVal ar As IAsyncResult)
137 Dim bytesReceived As Integer = m_socket.EndReceive(ar)
138 If (bytesReceived > 0) Then
139 Dim msg As String = Encoding.UTF8.GetString(m_buffer, 0, bytesReceived)
140 Me.Invoke(m_socketDelegate, msg)
142 m_async = m_socket.BeginReceive(m_buffer, 0, m_buffer.Length, SocketFlags.Partial, New AsyncCallback(AddressOf OnReceive), Me)
146 Public Class PeerData
147 Public InterfaceIndex As UInteger
148 Public Name As String
149 Public Type As String
150 Public Domain As String
151 Public Address As IPAddress
152 Public Port As UShort
154 Overrides Function ToString() As String