Socket Chat Part 2: Internet Explorer Control Client
Add Comment<div align="center"> <table width="90%" class="outline"> <tr> <td width="30%" class="outline"><b>Download</b></td> <td width="15%" class="outline"><b>SDK</b></td> </tr> <tr> <td width="30%" class="outline"> <a class="wbox" href="../../file/socketchat.zip">socketchat.zip</a> (41kb)</td> <td width="15%" class="outline">Beta2</td> </tr> </table> </div> <p> <span class=wboxheado>Introduction</span><br> In the <a class="wbox" href="article.aspx?ArticleID=68&&TopicID=14">last part</a>, I presented the <b>Socket Chat Client-Server </b>code. The previous example was purely an rich <i>Windows Form Application</i>. 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.</p> <p><span class=wboxheado>Windows Forms Controls within Internet Explorer</span><br> 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.<br> Also before this question raises in your mind, I would like to clarify that the client system that will view this Control <b>should have</b> 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 <a class="wbox" target="_blank" href="http://msdn.microsoft.com/net"> http://msdn.microsoft.com/net</a> ).<br> <br> 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.</p> <p><span class=wboxheado>Screen Shot</span><br> <img border="0" src="../../img/socketchatcontrol1.gif" width="476" height="353"><br> <b>Figure 1:</b> Socket Chat User Control hosted in Internet Explorer</p> <p><span class=wboxheado>BUG List</span><br> 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.</p> <p><span class=wboxheado>Example:</span></p> <p><span class=wboxhead>Step 1: Create the Windows Form User Control</span><br> To start of with the example the first step is to create the <b>Windows Form Control</b>. As mentioned before both <b>Custom Controls</b> and <b>User Controls</b> are supported. Since I wanted to use the existing Windows Form controls I chose to create a <i>User Control</i>. If you need more information about creating User Controls read <a class="wbox" href="article.aspx?ArticleID=62&&TopicID=8">this article</a>.<br> 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. <br> One interesting thing I found out that if you use colors from the <b>System.Drawing.SystemColors</b> namespace then they <i>won't work</i>! By default all colors of the Windows Form Controls is defined from the <i>System.Drawing.SystemColors</i> so all your Controls will get drawn in white! <br> Hence you have to specifically set colors from the <b>System.Drawing.Color</b> class on all your controls. Once you finish coding your control compile it into a <i>library Dll</i>, I have used the command-line compiler statement to create the library <b>SocketUserControl.dll</b>:<br> <b>csc /t:library SocketUserControl.cs</b></p> <p>Please note that in my example I have set the control to connect to the <i>Server</i> at <b>localhost</b>. 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 <i>Socket Chat Server</i> in place of <b>localhost</b>. Remember that this is similar to the<b> ActiveX</b> controls and the code gets downloaded and executed on the <i>client system</i>. 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!</p> <p><span class=wboxhead>Step 2: Create a Virtual Directory</span><br> The next step is to create a Virtual Directory called <b>SocketControl</b>. Also I am assuming that the <i>SocketControl</i> Virtual Directory you just created is accessible from <i>http://localhost/SocketControl</i> URL.<br> 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.</p> <p><span class=wboxhead>Step 3: Copy the User Control to the Virtual Directory</span><br> Once the Virtual Directory is setup, copy the <b>SocketUserControl.dll</b> library into the <b>SocketControl</b> Virtual Directory. <br> Note here that you <b>do not</b> have to copy this dll into the <b>bin</b> 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.<br> So say you have the Virtual Directory with the physical path <i>c:\Inetpub\wwwroot\SocketControl</i>, then the library dll should be accessible from the same <i>c:\Inetpub\wwwroot\SocketControl\SocketUserControl.dll</i> path.</p> <p><span class=wboxhead>Step 4: Create the Web Page</span><br> .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 <b>Default.htm</b>, but can create a ASP.NET web page if you wish.</p> <p><b>Default.htm</b> - The Control host page.</p> <table width="100%" class="code"> <tr> <td > <pre><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></pre> </td> </tr> </table> <p>The above page is a simple HTML page with just one <b>OBJECT</b> tag defined. All the properties except the <b>classid</b> property is pretty self-explanatory. Unlike <i>ActiveX </i>controls you don't have to specify an <b>GUID</b> for the <i>OBJECT</i>. <br> If you observe the <i>classid</i> property you will find that it is a simple <i>URI</i> to the control we are hosting followed by a pound sign <b>#</b>. After the pound sign we specify the<i> fully qualified name</i> 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.<br> All users hosting this example in an Intranet/Internet environment or users deploying this example to some other path should note that the <i>classid</i> property takes the publicly accessible URL of the control. i.e. the path to the control should be accessible from your Intranet/Internet.<br> 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 <i>localhost</i>, i.e. the same client computer that's accessing the page via Intranet/Internet and the control will fail to load!<br> So don't forget to make necessary changes to the controls URI!!<p><span class=wboxhead>Step 5: Testing the example</span><br> The above steps complete the installation of the example, now its time for the fun ... testing part of the application. First run the <i> Socket Chat Server</i> and then open <i>Internet Explorer</i> and enter the path to the Html file we just created. i.e. <i> http://localhost/SocketControl/Default.htm</i>. If your installation went properly then the Control should show-up in your browser... <br> Next, happy chatting .... :) <p><span class=wboxhead>Step 6: Dealing with Security (Updated 16 December 2001)</span><br> I have seen in the <i>feedback</i> section that many of you are having problems working with this, since when try to connect to a <b>Socket Chat Server</b> hosted on your local server using the <b>Socket Chat Control</b> hosted in Internet Explorer you get a <b>Security Exception</b> thrown and the connection fails :(. This is due to the fact that the <b>.NET Runtime Security</b> 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 <b>caspol.exe</b> (Command-line) tool or the <b>mscorcfg.msc</b> mmc snap-in to manually '<b>Trust</b>' 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 <i>Socket Chat v2 Client-Server</i> series coming soon!! So stay tuned.... Anyways you could read <a class="wbox" target="_blank" href="article.aspx?ArticleID=72&&TopicID=8">this article</a> which shows how to deal with .NET Security with the help of another example. <p><span class=wboxheado>Conclusion</span><br> 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.<br> 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!!