Library tutorials & articles
Easy networking without Sockets or Remoting
- Simple chat client
- Simple chat server
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.
Related articles
Related discussion
-
hey developers out there
by pitsophera (0 replies)
-
Capture a Screen Shot
by impalax (1 replies)
-
Selecting Multiple Line using Mouse Event?
by morizan (2 replies)
-
How can i develop opc server using .net?
by vairajaig (1 replies)
-
need a help for multithreading in vb.net
by morizan (1 replies)
Related podcasts
-
More jQuery in ASP.NET
In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, and Scott Koon conclude their discussion of Microsoft's jQuery in ASP.NET announcement1.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are ...
Events coming up
-
Nov
18
15 Minutes of Fame
Dresher, United States
This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.
This thread is for discussions of Easy networking without Sockets or Remoting.