This resource has not currently been approved, and is not currently linked to from our directory of resources. It is being displayed here for preview by the author and moderators only.
Simple chat server
Private WithEvents serverconn As foxtrot.xray.Announcer.Server
After defining our Server object, we will create a new instance of it. The server
is a tad more complex in its initialization: it requires a service identifier,
a port to use for listening to incoming connections, and a TTL for the service
broadcast (this defines how many router boundaries your service broadcast will cross).
serverconn = New foxtrot.xray.Announcer.Server("ChatServer v1.0", 2000, 2)
Now, we add handlers for the ClientConnected and ClientDisconnected events, to
add and remove users from the list when they connect or disconnect:
Private Sub serverconn_ClientConnected(ByVal client As _
foxtrot.xray.Announcer.Server.ClientConnection) _
Handles serverconn.ClientConnected
' the ClientID property of the ClientConnection contains a string that
' can be used as the unique ID of the Client
AddClient(client.ClientID)
End Sub
Private Sub serverconn_ClientDisconnected(ByVal client _
As foxtrot.xray.Announcer.Server.ClientConnection) _
Handles serverconn.ClientDisconnected
' the ClientID property used as a unique identifier
RemoveClient(client.ClientID)
End Sub
Now, we add a handler for the DataReceived event, to handle all the messages
coming from clients:
Private Sub serverconn_DataReceived(ByVal message As _
foxtrot.xray.Announcer.Server.ClientMessage) _
Handles serverconn.DataReceived
Dim rdata As New Hashtable
Dim sdata As New Hashtable
Dim nick as string
message.GetData(rdata)
Dim connections() As foxtrot.xray.Announcer.Server.ClientConnection
Select Case rdata("op")
Case "nickset"
' look in the list of nicknames for requested nick
' If nick is found
sdata("result") = "inuse"
' Else, this is a new nick
sdata("result") = "ok"
' store the new nick in the list, and associate it to
' the ClientID property of the Client connection
' End If
' Send the reply
message.Send(sdata)
Case "chat"
' Get the nickname associated with the ClientID of this
' connection and store it in the nick variable
rdata("nick") = nick
' Get the list of Clients connected to this server
connections = serverconn.Clients()
' Forward the chat text to all clients
If Not (connections Is Nothing) Then
For i As Integer = 0 To connections.Length - 1
connections(i).Send(rdata)
Next
End If
End Select
End Sub
We have successfully implemented a networked application without using
any actual networking code.