Chat, Using iTransferable Objects

back to iNetwork


Prior to beginning this tutorial example, we recommend that you take the time to read the brief discussion regarding iTransferable Objects, which is a feature provided by the toolkit.

back to top



This Chat, Using iTransferable Objects tutorial example is a revised version of and therefore works with its the code and composition.

The functionality of this version does not differ from that of its previous version. It simulates an instant messaging environment using the iNetwork toolkit.

High Level Overview

When a user enters an instant message a message is sent from the client to the serer. In this message to the server, however, instead of adding the username string and the message string directly, a ChatMessage object is instantiated instead. This object is what is then added as a field to the message sent to the server. In return the server broadcasts this message to all clients. At each client the ChatMessage object is retrieved in order to get the username and instant message.

Reminder: The executable file, called iTransferrableExample, for this example has been provided in a folder name Tutorial2iTransferable when the toolkit was installed. You will find this in the Tutorial Examples folder within the GroupLab iNetwork Toolkit directory. Be sure to extract the files before opening them.

back to top

Changing the Code

This part of the tutorial will show the changes made to the code provided in the Chat example.


Before you can change anything with the pre-existing code, first create a new class within the client project.

To do this simply right click on the ChatClient project and select Add then New Item. In the Add New Item window be sure to select the Class template and name this ChatMessage.

Open the C# file for the ChatMessage class.

  1. Begin by making the class explicitly public and have it inherit the iTransferable interface.
    public class ChatMessage : ITransferable {...}
Class Instance Variables Region
  1. Create two string variables. _username will be used to track the client's username and _message will be used to track the instant message text.
    private string _username;
    private string _message;
Constructor Region
  1. Two constructors need be made for this class. The first is the basic constructor and the other the secondary constructor.
    1. public ChatMessage(string username, string message)
    2. {...}
    4. public ChatMessage(NetworkStreamInfo info)
    5. {...}
  2. In the basic constructor, assign the values from the parameters to the two instance variables you created previously [line 2, step 3].
    1. this._username = username;
    2. this._message = message;
  3. The secondary constructor uses the NetworkStreamInfo to get the two values that you need [line 5, step 3]. Both user and message are the names of the values that each correspond to. These values are tracked by the NetworkStreamInfo.
    1. this._username = info.GetString("user");
    2. this._message = info.GetString("message");
Methods Region
  1. Having inherited the iTransferable interface, the ChatMessage class needs to implement the GetStreamData method. Using the NetworkStreamInfo add the value of this._username and this._message, given the names user and message respectively.
    1. public void GetStreamData(NetworkStreamInfo info)
    2. {
    3.     info.AddValue("user", this._username);
    4.     info.AddValue("message", this._message);
    5. }
  2. Create a GetUsername method that simply return the string username.
    1. public string GetUsername()
    2. {
    3.     return this._username;
    4. }
  3. Create a GetMessage method that simply return the string message.
    1. public string GetMessage()
    2. {
    3.     return this._message;
    4. }
back to top


Open up the C# file for the client.

Sending Chat Messages Region
  1. In the SendMessage method replace all the code within the if statement with the code shown in the next couple of steps. First, create a ChatMessage object.
    1. ChatMessage chatMsg = new ChatMessage(this._username, messageToSend);
  2. Create a new Message and add the chatmsg as a field.
    1. Message message = new Message("ChatMessage");
    2. message.AddField("chatmsg", chatMsg);
  3. Finally send the message through the connection and clear the _messageText textbox.
    1. this._connection.SendMessage(message);
    3. this._messageText.Text = "";
  4. Now, in the OnMessageReceived event handler method, find the ChatMessage case. Within this block, replace all the code before the if statement with the code shown in the next couple of steps. Again, first create a ChatMessage object by retrieving the the field added to the message.
    1. ChatMessage chat = msg.GetField("chatMsg", typeof(ChatMessage)) as ChatMessage;
  5. Using the get methods you created in the ChatMessage class retrieve the string username and message text.
    1. string username = chat.GetUsername();
    2. string text = chat.GetMessage();
back to top


Reminder: Ensure that the server is run before the client. Right click on ChatServer in Solution Explorer and select Set as StartUp Project. To run/add instances of the client simply right click on ChatClient in the Solution Explorer. Select Debug then Start a new instance.

When you run the application, what you get should be exactly the same as ChatClient only this time you have demonstrated the use of iTrasnferable objects.

back to top