If you haven't read the first tutorial (A Client server Chat program using Winsock control tutorial) - please do so now. This VB6 tutorial builds off of the previous one. This tutorial differs from the first one in that you can have more than one client connect at a time. This is similar to how IRC works.
We will not need to change anything in the client portion of our program. It behaves in the exact same way. However, in the server side of our program we have to get a little creative. If you remember in the last tutorial we simply disconnected any pervious connects and accepted the new connection in the Winsock controls ConnectionRequest event. Now instead of doing this we will allow multiple connections to occur. To handle that we will be creating a control array of Winsock controls. Then when a connection comes in on our main socket we will create a new Winsock control in our control array and assign the connection to it. If this sounds confusing just follow along and it will make much more sense. First set the index property of sockMain on Form2 to 0. This tells VB6 that sockMain is now a control array. This will allow us to create more controls at runtime.
Also download the source code if you want to follow along with this tutorial.
To begin open up Form2's code window. We will be adding a global variable that will keep track of the number of connections we have.
Option Explicit Private intSockCnt As IntegerNext delete any of our old code we will be recreating it. Now lets start with the tricky part first - sockMains connectionRequest event. If you notice the sockMain_ConnectionRequest now has two parameters instead of one. The new parameter index. This index value will tell us what sockMain in our array of sockets had a connection request. Add this code in this event.
Private Sub sockMain_ConnectionRequest(Index As Integer, _ ByVal requestID As Long) intSockCnt = intSockCnt + 1 Load sockMain(intSockCnt) sockMain(intSockCnt).Accept requestID txtStatus.Text = txtStatus.Text & _ "Connected to: " & _ sockMain(intSockCnt).RemoteHostIP & vbCrLf End Sub
This code is similar to what we did in our last tutorial. However, instead of simply accepting the connection and killing any old ones, we load a new Winsock control and accept the connection on that one instead. This is where the requestID comes in handy. Because when we load our new Winsock control it doesn't know anything about the connection attempt made to our old control. However, when we call the Accept method on our new control and pass it this requestID it will take over the rest of the processing from that client.
Now lets write the code to handle data arriving.
Private Sub sockMain_DataArrival(Index As Integer, _ ByVal bytesTotal As Long) Dim strData As String Dim intCnt As Integer sockMain(Index).GetData strData, vbString txtStatus.Text = txtStatus.Text & _ strData & vbCrLf 'This sends the data back to the other clients For intCnt = 1 To intSockCnt If sockMain(Index).State = sckConnected Then sockMain(Index).SendData strData End If Next intCnt End Sub
Again notice we have the handy index property and again notice our code is very similar to the previous tutorials code. The only difference is that we use the index variable and we resend the data back out to any other clients that are connected. We do this with the For loop. We loop through our array of connections. If they are still connected then we send them the data.
Next lets wire up our Listen and send command buttons.
Private Sub cmdListen_Click() sockMain(0).Close sockMain(0).LocalPort = txtPort.Text sockMain(0).Listen End Sub Private Sub cmdSend_Click() Dim intCnt As Integer 'This sends the data back to the other clients For intCnt = 1 To intSockCnt If sockMain(intCnt).State = sckConnected Then sockMain(intCnt).SendData txtSend.Text End If Next intCnt End Sub
This is almost exactly like our last tutorial. The only difference in cmdListen_Click is that we are specifing our array index of 0 in our listen event - this is the main socket control we created on Form2. The other difference in our cmdSend_Click is that we loop through all our open connections and send the data to them.
It is a bit tricky to test this out as we need two clients. I recommend compiling the program as an exe. Than you can run it more than once. Just remember to only have one server (form2) listening at a time - than you can have as many clients (form1) as you want connect to them.
There you have it. Now to make this a working program you will want to add some better error handling, but you get the idea from this tutorial. Also download the source code for this tutorial to see a working example.