By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

Chat Application Using .NET C# Remoting

P: 8
Hi , Could someone let me know how to start with the building a chat client server application . I have tried the basic remoting sample wherein the client contacts the server n gets a response . I wanted to proceed towards building a chat application . I need help with what should the client do , example what would be the remote object here and how is it manipulated ? what would the client n server code deal with ?
Thanking in advance ! :)
Feb 9 '09 #1
Share this Question
Share on Google+
9 Replies


vekipeki
Expert 100+
P: 229
There are many samples on the web, try googling for "chat remoting .net".
Feb 9 '09 #2

P: 8
Hi ,
Hmm yes i did find one sample example . I went thru teh code as well .However i have one concern . Inremtoing they say there are 2 modes , SingleCall n SinglTon mode for handling the remote objects (whether different instance or same instance is used per client). Iam not too clear with this.
In the Chat application that i mentioned , i have a server and client . Both are winforms . Upon starting server , the form is loaded which is a normla chat window . When Client is started , we get a similar chat window , i expect to type in msg , upon clicking send , the msg is displayed in own client window plus it also needs to get displayed in the chat window for the server .
That doesn seem to happen .
In the code for server , whenever user types in data in its chat window , it raises an event thru the mehod of the remote object + displays teh msg in its own window. The client at teh oder hand subscribes to teh event raised by the remote object n is coded to receive the msg and display in its window. This is so taht it displays what the server has just sent (thru thr remote object). The config file specifies Singleton as the mode . I thought this would mean that teh same remote object that is instantiated is offered to all the clients.
PLease note i have instantiation of remote object using new in both server n client . Does taht need to be changed ?

Server code :
namespace ServerChatApplication
{
public partial class Server : Form
{
private RemoteObject.RemoteObject remObjServerHandle;
public Server()
{
InitializeComponent();
InitializeData();
}

private void InitializeData()
{
System.Runtime.Remoting.RemotingConfiguration.
Configure("Server.exe.config", false);

//get handle of remote object
remObjServerHandle = new RemoteObject.RemoteObject();
remObjServerHandle.evtReceiveText += new RemoteObject.RemoteObject.ReceiveText(remObjServer Handle_evtReceiveText);

Console.WriteLine("Chat Server Started. ");
Console.ReadLine();

}

void remObjServerHandle_evtReceiveText(string username, string text)
{
//here populate the data in the text box ...sever also listens to teh event
//so dat it can take in wht the client sends
this.richTextBox2.AppendText(username + text);
}


private void ServerSend_Click(object sender, EventArgs e)
{
//raise the event thru the remote object ...so dat client can print this
this.remObjServerHandle.RaiseReceivedTextEvent("Se rver1 : ", this.richTextBox1.Text);
this.richTextBox1.Clear();

}

}
}


Client code :
namespace ClientChatApp
{
public partial class Client : Form
{
private RemoteObject.RemoteObject remObjClientHandle;

public Client()
{
InitializeComponent();
InitializeData();

}

private void InitializeData()
{
System.Runtime.Remoting.RemotingConfiguration.
Configure("Client.exe.config", false);

//get handle of remote object
remObjClientHandle = new RemoteObject.RemoteObject();

remObjClientHandle.evtReceiveText += new RemoteObject.RemoteObject.ReceiveText(remObjClient Handle_evtReceiveText);

Console.WriteLine("Chat Server Started. ");
Console.ReadLine();
}

void remObjClientHandle_evtReceiveText(string username, string text)
{
//populate the text box with the data
this.richTextBox1.AppendText(username + ":"+text);
}

private void ClientSend_Click(object sender, EventArgs e)
{
//raise the event thru the remote object...so dat server can print dis..
this.remObjClientHandle.RaiseReceivedTextEvent(Sys tem.Windows.Forms.SystemInformation.ComputerName, this.richTextBox2.Text);
this.richTextBox2.Clear();
}
}
}


RemoteObject Code :
public class RemoteObject:MarshalByRefObject
{


// Contains all delegate and events
public delegate void ReceiveText(string username, string text);
public delegate void UserAdded(string username);
public delegate void UserRemoved(string username);


public event UserAdded evtUserAdded;
public event ReceiveText evtReceiveText;
public event UserRemoved evtUserRemoved;




public RemoteObject()
{
//
// TODO: Add constructor logic here
//
}



// Contains all public methods of the class
//this method raises the event , subscribed by server + client
public void RaiseReceivedTextEvent(string username, string text)
{
evtReceiveText(username, text);
}


}
Feb 12 '09 #3

Expert 100+
P: 750
PRR
Do look into WCF... here an article about chat application by Sacha Barber.. i would suggest that you read on remoting or WCF before you start with project... these are advanced topics...WCF is a better choice...
Feb 12 '09 #4

P: 8
HI , thanks for the link ,
but i took a slight detour from the orginal plan . Instead of goin for something as complex as a chat program , i wanted to try out something simple.
To test remoting , i just created chat , server n remote object and tried accessing remote object methods from teh client which worked fine . Then i wanted to implement the same using events . For this , i created a delegate n an event in remote object , for which teh client has a subscription . The event is raised n is received fine by the client however ders a small glitch . Inside the event handler code of the client , iam trying to set the vale of a particulat textbox on the client window to a certain value . The application seems to hang at thsi place and the watch window gives an erro sumthing like :
Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.}

it shows the smae for all the controls on the client window form . I googled for the same n most of the blogs talkmention dat sometimes the thread dat evaluates the object , it takes longer ...so watches shud be avoided n de also mentioned some setting to avoid evaluation of ToString methods during execution . One of the posts i stumbled upon spoke abt using teh follwoing statement above the control m accessing:
[System.Diagnostics.DebuggerBrowsable(System.Diagno stics.DebuggerBrowsableState.Never)]

which basically does the same thing by marking these controls as non browsable . However this didn solve my problem .
i also cross verified by declaring a delegate n event in my client class n listening to the same in an event handler . If i access any control inside that theres no problem , also if i access any control inside any oder method also der is no problem . The problem arises only if i try to access the controls in the event handler for the remote object event . Currently its not even consisting of any input parameters ,,,a very simple implemenation like...
//event handler caters to event raised by remote object
public void remObject_ModifyEvent()
{

MessageBox.Show("Event received");
MessageBox.Show(valuetextBox.Text);//hangs at this line...

}

Can sumbody pls pls help me wid this??? its urgent !!

Thanks in advance !
Feb 18 '09 #5

P: 8
OK the latest i have in hand is this ,
The evnt handler code is started on a different thread than the main UI thread , n when iam trying to access the UI controls on this thread , it hangs since UI controls can be accessed only on main thread .
For this , i tried using invoke using a delegate which calls a method which in retun accesses the control value. BUt alas , now the code hangs at the invoke statement itself....
//this is the event handler in the client
public void remObject_ModifyEvent()
{
MessageBox.Show("Event received");
System.Diagnostics.Trace.WriteLine("remObject_Modi fyEvent " + Thread.CurrentThread.ManagedThreadId);
if (this.InvokeRequired == true)
{
string text = this.Invoke(m_TextDelegate) as string; //hangs here
MessageBox.Show(text);

}
}


the delegate is defined as :
public delegate string GetTextDelegate();

GetTextDelegate m_TextDelegate;

The delegate is assigned to the method name :
m_TextDelegate = GetText ;

n the method is :

public string GetText()
{
return valuetextBox.Text;
}
Feb 20 '09 #6

vekipeki
Expert 100+
P: 229

P: 8
i understand wht he is saying ...n i have even tried it already , but still it hangs ..
the thing is upon receiving an event , i need to get the value of the control on the main thread (using begin invoke) n display it ....now i cant display until i get the results back from teh asynch call....so any ways its same as invoke wherein i have to wait for the result to ven proceed further .
I thik dis is one of the basic problems neone wud have faced . Iam not sure why ders no def solution to dis ?
Feb 23 '09 #8

vekipeki
Expert 100+
P: 229
A simple way would be to add an event handler for the TextBox.TextChanged event, and in that handler change the value of a private volatile field which will contain the current string. Then you can access that field from any thread without the need to sync with Gui thread.
Feb 23 '09 #9

P: n/a
SysnchronizationContext of UI thread is required if you want to set any UI control's value from another thread. The SysnchronizationContext should be passed as a parameter to the function which executes on the other thread.

-Sagar
Oct 25 '10 #10

Post your reply

Sign in to post your reply or Sign up for a free account.