I'm working on a "chat" application to learn DotNet remoting and I have
reviewed several examples, but I'm having a problem...
When the client is being initialized I instantiate the remote object (works
fine), then access a property on the server object to get the machine name
(works fine) , then call a method on the server object to add that machine
name to the list of clients (works fine), then the server fires an event
which is handled by the client (works fine), THEN at this point the client
just hands.... and the client "chat" form is never displayed.
I have three components (MyChatClient.exe, MyChatServer.exe, and
MyChatService.dll) and here is the pertinent information from each... Note:
I've snipped alot of code from the client and server in order to keep the
message short... I can post all the code if necessary....
----------------------------------------------------------------------------
MyChatServer --- just loads MyChatService
----------------------------------------------------------------------------
----------------------------------------------------------------------------
MyChatService
----------------------------------------------------------------------------
using System;
using System.Collections;
using System.Diagnostics;
namespace MyChat
{
public delegate void UserAdded(string username);
public class ChatService : MarshalByRefObject
{
public event UserAdded evtUserAdded;
//array list of user who are currently logged into the chat
protected ArrayList Users;
//initialization
public ChatService()
{
Users = new System.Collections.ArrayList();
}
public override object InitializeLifetimeService()
{
return null;
}
public void AddUser(string name)
{
//add to Users list
Users.Add(name);
//fire this event
evtUserAdded(name);
}
public string GetServerName
{
get
{
return System.Windows.Forms.SystemInformation.ComputerName.ToString();
}
}
}
}
----------------------------------------------------------------------------
MyChatClient
----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using MyChat;
public class Form1 : System.Windows.Forms.Form
{
//set reference to chat dll
public ChatService oChat;
private string m_MachineName;
public Form1()
{
// Required for Windows Form Designer support
InitializeComponent();
//Connect remotely with config file
RemotingConfiguration.Configure("MyChatClient.exe.config");
oChat = new ChatService();
//get machine name for this client
m_MachineName = oChat.GetServerName;
//assign the event handlers for the ChatService events
oChat.evtUserAdded += new MyChat.UserAdded(this.UserAdded );
//inform server we have logged on
oChat.AddUser (m_MachineName);
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public void UserAdded(string name)
{
UpdateUserText(name);
}
private void UpdateUserText(string str)
{
txtUsers.Clear();
txtUsers.AppendText((string)sb.ToString());
}
}
Again, the client app hangs after the "UserAdded" function runs on the
client... the Form is never displayed. If I stop and run the Client app
again I get the following error "Unable to connect to remote server".
Here is the pertinent info from the config files:
CLIENT CONFIG FILE
<client>
<wellknown type="MyChat.ChatService, MyChatService"
url="http://ieckvdgu902:9876/MyChatServer" />
</client>
<channels>
<channel ref="http" port="5555">
<serverProviders>
<formatter ref="soap" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>
SERVER CONFIG FILE
<service>
<wellknown mode="Singleton"
type="MyChat.ChatService, MyChatService"
objectUri="MyChatServer" />
</service>
<channels>
<channel ref="http" port="9876">
<serverProviders>
<formatter ref="soap" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>
Me
PS: I realize some of this code is not using "best practices" but I'm just
trying to learn something here.