Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

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.
Rated
Read 5,180 times

Contents

Downloads

Related Categories

Easy networking without Sockets or Remoting - Simple chat client

fsanchez

Simple chat client

Your applications can communicate on a local area network quite easily, by using the Announcer v1.0 component for .NET. Using the Announcer component, you can turn any application into a network server or client with minimal coding effort.

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.

Comments