Library tutorials & articles

WinChat For .NET

A few points to discuss

In this simple project, I have made use of only a few namespaces:

System.Windows.Forms;
System.Net.Sockets;
System.Threading;
System.Timers;

I found that these namespaces really make the project much simpler than it would have been if it is done in C++. In addition, the delegate concept also makes the event-driven model much easier to handle than C++.

Most of the program flow and techniques used are already documented inside the code as comments. However, there are a few points that I think will be worth discussing here.

First of all, I think the System.Net.Sockets provides a lot of great classes that make the Socket programming a joy to work with, especially the TcpListener, TcpClient and UdpClient classes. They take care of most of the low-level socket programming under the hood and make their operation transparent to the client code. For example, to create a TCP server and make it listen to the port TCP_PORT, all I have to do is only 1 line of code:

TcpListener tcpListener = new TcpListener(TCP_PORT).

And to create a TCP client and make a connection to the TCP server SERVER_NAME at TCP_PORT, again, 1 line of code will do the job:

TcpClient tcpClient = new TcpClient(SERVER_NAME, TCP_PORT).

Although all these classes still perform the traditional socket operations under the hood, hiding all of them from the client application does help simplify the overall program structure. However, for those programmers who are familiar with Winsock programming, I still suggest to use the traditional Socket class because it's still more powerful then these "convenient" siblings.

Second, this project has made use of the Multi-Threading facilities provided by the .NET platform. The UDP server and the TCP server are running on different threads so that they can receive and process incoming messages simultaneously. The following code segments show how the TCP and UDP threads can be started and aborted:

public void start_servers()
{
	//Starting the TCP Listener thread (StartListen).
	sampleTcpThread = new Thread(new 
			ThreadStart(this.StartListen));

	sampleTcpThread.Start();
				
	//Starting the UDP Notify thread (receiveNotify).
	udpNotifyThread = new Thread(new 
			ThreadStart(this.receiveNotify));

	udpNotifyThread.Start();
}


To start the server threads:

start_servers();

To abort them:

sampleTcpThread.Abort();
udpNotifyThread.Abort();

This is it! Of course, you might want to add some try/catch blocks to handle some exceptions that might happen in the codes.

The third thing that I want to discuss is the delegate model of event handling. In this project, there are quite a few places that I can make use of delegates. For example, in Fig 1, step 6, after the calling side connects to the called side's TcpUdpServer, the TcpUdpServer will raise an event. The event will then call the delegate defined in the WinChatFormLibrary class, which will, through the TcpUdpClient, make a connection to the calling side's TcpUdpServer. Here is the code segment that does what I described.

In WinChatFormLibrary class, we first have to make the TcpUdpServer listen to the event peerNotify():

TcpUdpServer.peerNotify += new
TcpUdpServer.NotificationHandler(this.OnPeerNotify);

Defining the OnPeerNotify delegate (with most details deleted):

public void OnPeerNotify()
{
    this.tcpUdpClient.createTcpClientConnection(remoteHostName};
}

In the TcpUdpServer, all we have to do is to call the event peerNotify() when it receives a connection from the remote peer. And it's done!

Last but not least, I also want to briefly mention the timer handling in the .NET platform. I have made use of a few timers in this project. To start a timer, all I have to do is just 2 steps:

1. Add the event listener to the code where you want to start the timer:

receiveNotifyTimer = new
System.Timers.Timer(30000);
receiveNotifyTimer.Elapsed += new
ElapsedEventHandler(this.OnTimedEvent);

2. Define the delegate OnTimeEvent():

public void OnTimedEvent1(object source, ElapsedEventArgs e)
{
    //Do something when the timer expires.
}

And stopping a timer is just a matter of calling someTimer.Stop(). That's it!!

Comments

  1. 09 Sep 2008 at 04:57

     WinChat (Patrick Lam) looks like .NET 1.1.  You will no doubt get cross-thread errors between GUI and threads processing client/server text.  Recommend you look up delegates to get rid of this type of error.  .NET 1.1 did not flag this error, but .NET 2.0 correctly flags the error.   I am in the process of doing this now.  chuck[quote user="Developer Fusion Bot"]

    This thread is for discussions of WinChat For .NET.

    [/quote]
  2. 31 Aug 2007 at 05:36

    It didn't work right out of the box and I have Dot NET 2.0 installed but not any IDEs at this time.

    Could it have been written in an older version and I need to convert it? I've had this happen before but had to use the IDE to load the solution and let it upgrade.

    It was just a test anyway as I was searching for information about the one installed in XP. It seems a bit obscure and I have never connected to anyone with it. I need to know someones computer name.

    I've never liked downloading applications that I know nothing about. The browser based chat apps are nice in this respect. Many are based on Java applets. The only thing that you have to make sure you have is a working version of  a Java Runtime Environment(jre). I have the latest build jre1.6.0_02.

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of WinChat For .NET.

Leave a comment

Sign in or Join us (it's free).

Patrick Lam
AddThis

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

We'd love to hear what you think! Submit ideas or give us feedback