How to C# Socket Programming using Visual Studio.Net

Learn step by step network programming using C#.Net socket, remoting, WCF and SignalR technology from lots of source code and description. All source code download link available at end of the blog post.

Global Text Chat Room Application using C#.Net Programming - Remoting technology 3/6

ß Read Previous



Chat room window has four sections largest one to see all chat message and below of that to type chat message, and send button to send message to server. Just above list to display all online user.

When you put your name then client application creates a remote base class’s object and connects to server by registering TCP channel. Then connects to Chat Room and seek latest message number. After that main Chat Room window opens. From that window it seeks latest available message in server by a timer. To get message from server it invokes ‘GetMsgFromSvr()’, and get available online user through ‘GetOnlineUser (), and user message sends from client application to server by invoking ‘SendMsgToSvr()’. For better understanding you may go through the code.

Source code as below and full source code can download from following link:

https://rapidshare.com/files/276755910/GChat_RemoteBase.zip


How the code is working


Start the Server:

When users are trying start Server by clicking Start button the following code executes –


private void btnStart_Click(object sender, EventArgs e)

{
if (channel == null)
{
channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(SampleObject), "ChatRoom"WellKnownObjectMode.Singleton);
lblStatus.Text = "Running...";
btnStart.Enabled = false;
btnStop.Enabled = true;
}
}
Here a TcpChannel opens with port number 8080 and register it as WellKnownServiceType. ‘ChatRoom’ it is the ‘ObjectUri’ it will require to connect to server from client.
On the other hand when user press to ‘Stop’ server then unregister the channel and stop the server. Codes are below-
private void btnStop_Click(object sender, EventArgs e)
{
if (channel != null)
{
ChannelServices.UnregisterChannel(channel);
channel = null;
lblStatus.Text = "Stopped.";
btnStart.Enabled = true;
btnStop.Enabled = false;
}

leave a response