Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
71 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 Page 1
 Page 2

 Printable version
 Discuss this article
 in the forums


Before you start, do yourself a favor and grab the code for this lesson.

Ok gang, here we are again with another interesting and unusual little project. In this article we'll discuss and build a simple chat server and client. The language we'll use will be VB. The reason for this is the very nice Winsock control that VB has. It allows us to code all the functionality and cover the concepts involved in multi-chat sessions without getting too confused in the actual implementation.

I've done a little investigation on the web and as it turns out it's very easy to find applications which describe client server interaction between a single client and single server. It's NOT so easy to find examples of chat applications that cater for MULTIPLE connections to the same machine. I'm talking specifically now about CHAT applications. What's the point of building a communication application if only 2 people can communicate. We want EVERYONE involved. So I decided to throw some code together to see what's involved in building this thing.

What's a chat application? Well it can be many things. When I think of a chat I think of Yahoo Messenger, AOL instant messenger or IRC. For anyone who's used NetMeeting you'll know you can also draw, type etc and send information such as files back and forward. That's all pretty advanced but built around a few simple concepts. For our purposes I'll try to keep things simple and just deal with text. Later on we'll discuss how you'd expand the applications we're going to build here to incorporate more advanced exchanges such as drawings etc.. As with all the Cornflake Articles, this project should lay the ground work for greater things to come. When you're done reading this be sure to send me some feedback on it. I'd love to hear how I can improve my writing style or explanations etc. Thanks now let's get going.

First of all we're going to need a strategy as to how we want communications to flow. I've already mentioned the words client and server but let's define those a little better.

The client is the application that the users' will use to connect to the server. The server is the application that will host the chat session and that all users will connect to. The communication will run through the system like so:

In our application our server will have 1 connection for itself. This connection will be the socket for all other clients to "plug-into" or connect to. Woah, what's a socket?

A socket is a method of connecting to a physical machine through it's network connection. We need an IP address and a Port number on that IP address. Ports are important in that certain things such as firewalls, routers and switches all can block or allow traffic on certain ports. For instance most webservers are hosted on port 80 of whatever machine they're running on. This doesn't have to be the case though, a webserver could run on any port so long as it's available. And that's the key word here, "available". Similarly, we'll need to chose a port to host our server on and ensure no other application is using it. For that reason I won't chose port 80, I'll pick a random port (1212). How do I know that's available? Answer: I don't until I try to start up the server I'm going to build. There's a virtually infinite number of ports I can pick so if this one is not available, I'll just pick another.

First we need to add the Winsock control to our list of components. Click "Project->Components" and select "Microsoft Winsock Control 6.0" from the list. That should add the following control to your toolbox:

We'll use three forms in this project. The client, the server and a simple loading form. We'll create all forms and controls right now so that we can then concentrate on the code instead of the GUI.



My forms look like this but you can arrange them anyway you want. Notice the placement of the Winsock controls on the Server and the Clients. Make sure you create a control array for the Winsock control onthe server. Do this by typing "0" in the "index" field of the Server Winsock Control properties.

The code for the startup form is really simple. It just sets the IP address and port for the server or client to listen on or connect to. It then shows the client or server depending on the button the users' pressed. It looks like this:

Option Explicit
Private Sub SetAddress()
  gPort = txtPort.Text
  gIPAddress = txtIPAddress.Text
End Sub

Private Sub cmdClient_Click()
  Call SetAddress
  frmClient.Show
  Unload Me
End Sub

Private Sub cmdServer_Click()
  Call SetAddress
  frmServer.Show
  Unload Me
End Sub

Let's note here that I'm using Option Explicit.

The first thing we're going to do is start our server Listening. We'll do this in the Form load event. Our server is going to listen on a specific port for our clients who want to connect. Here's the code for that:

Private Sub Form_Load()
  ' Only connection(0) listens, all others are connections
  sktConnection(0).Close
  sktConnection(0).LocalPort = gPort
  sktConnection(0).Listen
  ReDim gHandles(1) As String
  gHandles(0) = "Cornflake Server"
  AddToServerLog ("Server is Listening on port " & gPort)
End Sub




Next : Page 2