Library tutorials & articles
Easy networking without Sockets or Remoting
- Simple chat client
- Simple chat server
Simple chat client
Let's begin by creating a small chat client / server app. Let's start with the client:
Private WithEvents clientconn As foxtrot.xray.Announcer.Client
After defining our Client object, we create a new instance of it, providing a service identifier string that the Client will seek and connect to:
clientconn = New foxtrot.xray.Announcer.Client("ChatServer v1.0")
Our Client object will now handle connection and reconnection automatically, without requiring any further intervention or connection management. You will be notified when the Client connects to a server successfully, when data is received from a Server, and when the Client is disconnected from the Server.
Let's add a handler for the ClientConnected event:
Private Sub clientconn_ClientConnected() Handles clientconn.ClientConnected
Dim data As New Hashtable
Dim nick As String
' Ask for desired nickname to use on server, which will be placed
' on the nick variable
data("op") = "nickset"
data("value") = nick
clientconn.Send(data) ' send the nickname request
clientconn.Receive(data) ' receive response from server
If (data("result") <> "ok") Then
' This nickname is in use! Instruct user to select another!
Else
' The nickname has been accepted by the server!
' Enable the user interface and allow the client to send
' messages!
End If
End Sub
Upon connection, we immediately send the server a nickset request, and we wait for an answer. Do noticee that we send and receive data stored in a HashTable object directly: the Announcer component supports sending and receiving ISerializable objects, which means you can send many of the objects in the .NET Framework directly, or you can also create your own classes that derive from ISerializable and send them transparently.
Let's add a handler for the ClientDisconnected event:
Private Sub clientconn_ClientDisconnected() Handles clientconn.ClientDisconnected
' Disable the user interface
End Sub
For the sake of simplicity, in this tutorial we are not checking the return value of the Send or Receive methods, but you will notice their usage in the included project. The Announcer has a special flow-control mechanism which allows you to know with absolute certainty whether your message was delivered or not, without having to poll do any checking. The Announcer will also verify your data for you, so there is no need to add extra verification routines to ascertain the integrity of your data.
With that said, we will add code to send the lines entered by the user into the chat window, without waiting for a response.
Dim data As New Hashtable
data("op") = "chat"
data("value") = txtSend.Text
clientconn.Send(data)
The only remaining thing to do, is to add the code to handle when data is received from the server. For this, we shall add a handler to the DataReceived event:
Private Sub clientconn_DataReceived(ByVal message As _
foxtrot.xray.Announcer.Client.ServerMessage) Handles _
clientconn.DataReceived
Dim data As New Hashtable
dim nick as string
dim text as string
message.GetData(data)
nick = data("nick")
text = data("value")
' update the user interface with the received values
End Sub
Our simple chat client is now ready to go. On to the server.
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.