www.MasterCsharp.com Logo  AksTech Ad
 Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 

 

Socket Chat Part 2: Internet Explorer Control Client
   
 

 

[Rate this Article]
Download SDK Live Example
socketchat.zip (41kb) Beta2 Default.htm

Introduction
In the last part, I presented the Socket Chat Client-Server code. The previous example was purely an rich Windows Form Application. In this part I present one more client for Socket Chat, but this time the client is an .NET Control hosted within Internet Explorer. So remember this example will only run within Internet Explorer and other browsers that use the Internet Explorer engine.

Windows Forms Controls within Internet Explorer
The .NET Quick Start has a little example on this but nothing concrete. But the ability to host Windows Form Controls within Internet Explorer is indeed a powerful yet simple. You can host almost any kind of Control within Internet Explorer, i.e. you can host both Windows Form User Controls and Windows Form Custom Controls.
Also before this question raises in your mind, I would like to clarify that the client system that will view this Control should have the .NET Platform installed and Internet Explorer 5.5 above! Either the .NET SDK or the .NET Redistributable version (17 mb) should be installed on the clients machine (Both are available freely from http://msdn.microsoft.com/net ).

I have experienced problems with this in some versions of Internet Explorer. I was not able to load this control (or the one given the Quick Start's) with IE 6.0 beta. But I have tested it out on Windows 98 with .NET Redistributable and IE 5.5 as well as on Windows 2000 Professional with .NET SDK and IE 6.0 final.

Screen Shot

Figure 1: Socket Chat User Control hosted in Internet Explorer

BUG List
I am still having the same client disconnection bug, i.e if the client hit's the Disconnect button he does not get disconnected, also if he tires to reconnect then an exception will be thrown.

Example:

Step 1: Create the Windows Form User Control
To start of with the example the first step is to create the Windows Form Control. As mentioned before both Custom Controls and User Controls are supported. Since I wanted to use the existing Windows Form controls I chose to create a User Control. If you need more information about creating User Controls read this article.
I am not presenting the User Control code here, you can get it from the download file. The base code within it to communicate with the server is the same as that of the rich Windows Form client I created in the last article, I have just modified the GUI a bit.
One interesting thing I found out that if you use colors from the System.Drawing.SystemColors namespace then they won't work! By default all colors of the Windows Form Controls is defined from the System.Drawing.SystemColors so all your Controls will get drawn in white!
Hence you have to specifically set colors from the System.Drawing.Color class on all your controls. Once you finish coding your control compile it into a library Dll, I have used the command-line compiler statement to create the library SocketUserControl.dll:
csc /t:library SocketUserControl.cs

Please note that in my example I have set the control to connect to the Server at localhost. Incase you want to use this example in an Intranet or Internet environment, you will have to mention the machine name of the machine hosting the Socket Chat Server in place of localhost. Remember that this is similar to the ActiveX controls and the code gets downloaded and executed on the client system. If you don't change the above parameter then when the control gets loaded it will try to connect to localhost i.e. it will try to connect to the client system itself and fail!

Step 2: Create a Virtual Directory
The next step is to create a Virtual Directory called SocketControl. Also I am assuming that the SocketControl Virtual Directory you just created is accessible from http://localhost/SocketControl URL.
If you want to use another directory or are working with an external hosting, you can use a separate path but just remember to substitute your path everywhere I use the above mentioned path.

Step 3: Copy the User Control to the Virtual Directory
Once the Virtual Directory is setup, copy the SocketUserControl.dll library into the SocketControl Virtual Directory.
Note here that you do not have to copy this dll into the bin directory, where we usually place the library Dll's. On the contrary if you copy the dll into the bin directory the example will not work since contents from the bin directory are not publicly accessible.
So say you have the Virtual Directory with the physical path c:\Inetpub\wwwroot\SocketControl, then the library dll should be accessible from the same c:\Inetpub\wwwroot\SocketControl\SocketUserControl.dll path.

Step 4: Create the Web Page
.NET Windows Form Controls can be used from both plain Html pages or ASP.NET web pages. In my example I create plain html file called Default.htm, but can create a ASP.NET web page if you wish.

Default.htm - The Control host page.

<html>
  <head>
    <title>www.MasterCSharp.com - Master C#, the easy way ... - By Saurabh Nandu</title>
  </head>
  <body>
   <center>
     <h1>Master C# - Socket Chat</h1>
     <object id="SocketControl"
      height="312" width="580"
     classid=
 "http://localhost/SocketControl/SocketUserControl.dll#SocketControl.SocketUserControl">
     </object>
   </center>
  </body>
</html>

The above page is a simple HTML page with just one OBJECT tag defined. All the properties except the classid property is pretty self-explanatory. Unlike ActiveX controls you don't have to specify an GUID for the OBJECT.
If you observe the classid property you will find that it is a simple URI to the control we are hosting followed by a pound sign #. After the pound sign we specify the fully qualified name of the class the control should load. By fully qualified name I mean the class name along with the namespace within which the class resides.
All users hosting this example in an Intranet/Internet environment or users deploying this example to some other path should note that the classid property takes the publicly accessible URL of the control. i.e. the path to the control should be accessible from your Intranet/Internet.
To take an example, say you host the control with the same parameters I have used above. In this case if you access the page on the same computer that hosts the web page, then the control will show properly. But if you access the same page from some other machine in the Intranet/Internet then the page will again try to load the control from localhost, i.e. the same client computer that's accessing the page via Intranet/Internet and the control will fail to load!
So don't forget to make necessary changes to the controls URI!!

Step 5: Testing the example
The above steps complete the installation of the example, now its time for the fun ... testing part of the application. First run the Socket Chat Server and then open Internet Explorer and enter the path to the Html file we just created. i.e. http://localhost/SocketControl/Default.htm. If your installation went properly then the Control should show-up in your browser...
Next, happy chatting .... :)

Step 6: Dealing with Security (Updated 16 December 2001)
I have seen in the feedback section that many of you are having problems working with this, since when try to connect to a Socket Chat Server hosted on your local server using the Socket Chat Control hosted in Internet Explorer you get a Security Exception thrown and the connection fails :(. This is due to the fact that the .NET Runtime Security treats code downloaded from the Internet/ Intranet as potentially harmful code and runs it with its strict security policy which by default does not allow the code downloaded to open a socket connection. You will have to use the caspol.exe (Command-line) tool or the mscorcfg.msc mmc snap-in to manually 'Trust' your assembly downloaded over the Internet/Intranet. A full blown discussion on this is out of the scope of the current article, I will try to include it in the Socket Chat v2 Client-Server series coming soon!! So stay tuned.... Anyways you could read this article which shows how to deal with .NET Security with the help of another example.

Conclusion
In this article we saw how easy it is to host a Windows Form control within Internet Explorer. Even though this article revolves around my example, I have tried to outline all the important points you need to understand when you want to host your own controls.
Since my Socket Chat example is not feature rich, nor is it thoroughly tested I have not hosted it on my server, if someone does host this application on the internet please let me know your experiences!!

 

  
Saurabh Nandu - 16 December 2001

  Copyright © 2002 - 2004 MasterCSharp.com. All rights are reserved.

  Presenting MasterCSharp.com in association with AksTech Solutions - .NET Solutions Development and Consulting. 

  Best Viewed in IE 4.0+ and 800x600 Resolution