Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Thursday, November 20, 2008

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

Easy Networking without Sockets or Remoting
7/22/2006 4:53:06 PM

Your applications can communicate on a local area network quite easily, by using the Announcer v1.0 component for .NET (http://foxtrot-xray.com/main/prod/dev/announcer/) . For this tutorial, we will create a simple client-server chat application.

Author Name: Fernando Sanchez

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 notice 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.

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
     ' 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.

Download accompanying code.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating: No ratings available.
Comments Left:
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970