This content is not currently approved and is visible here for review only.

Library tutorials & articles

Easy networking without Sockets or Remoting

Page 1 of 2
  1. Simple chat client
  2. Simple chat server

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

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Easy networking without Sockets or Remoting.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related podcasts

  • CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly Media

    CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly MediaHosts Ken Levy and Markus Egger discuss the new State of .NET events, IE8, ASP.NET MVC, followed by an interview from PDC with two editors from O'Reilly Media. More on ASP.NET MVC can be found at http://asp.net/mvc. Interview...

Related jobs

Events coming up

  • Dec 6

    Developing AJAX Web Applications with Castle Monorail

    London, United Kingdom

    Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!