multiple clients

project 32
09-18-2001, 12:11 AM
i need to get multiple clients talking to each other, please do not post the MSDN help thing, ive tried that, i mean, its so vague, for example, at the end of the multiple connectin part it talks about "txtData(intmax)" but never tells you to make it, CONFUSING, can someone please tell me how to connect 2 of my clients to my server?

09-18-2001, 02:12 AM
To do multiple clients you need several things.
To start - you need two Winsocks.
One called Sock and the other should be called SockListen.
Sock should be a control array. (You can create a control array by copying Sock (select it and press Ctrl+C) then pasting another instance onto the form (click on the form and press Ctrl+V) . VB will ask you if you want to create a control array - click yes.)
Once you made Sock a controla rray you can delete the Sock you just pasted on, leaving you with one array called Sock, and one plain old sock called SockListen.

Now, basically, SockListen listens and the Sock array deals with the connections.
Every time SockListen receives its connection request, you accept it on a new Sock. In order to do this you have to load a new Sock.


Option Explicit
Dim iMax as Integer

Private Sub SockListen_ConnectionRequest(ByVal RequesID as long)
' accept the connection on a new Sock
iMax = iMax + 1
Load Sock(iMax)
Sock(iMax).Accept RequestID
' reset the SockListen
End Sub


This works cos when you load the new sock, Windows automatically assigns it a new port. This means that the port that SockListen is listening on is always available for incoming connections
iMax isa counter that keeps track of the length of the array. You have to increment it each time you load a new sock, so that the array is built like this
etc... the way any other array is numbered.

Hope this helps


"Junior Contributor?? Argh...... ***.. ***.... I preferred 'Centurion'! So much more...."

project 32
09-19-2001, 08:38 PM
for all the other procedures i have for example, WinServ_DataArrive should that now be Sock(intmax)_DataArrive and everywere i have used Winserv use Sock(intmax)?

09-20-2001, 12:10 PM
Yes the data arrival event should have the index property in there. The index is the reference to which Winsock the data arrived on. It should look like this:

Private Sub Winsock_DataArrival(Index as Integer, ByVal BytesTotal as Long)

(You dont have to call your Winsocks "Sock", you can leave them as "WinServ" if you want though...... You could call them "Cow" if you wanted. The only important bit is the counter - "intMax", and again that can be called anythign you want.)


"Junior Contributor?? Argh...... ***.. ***.... I preferred 'Centurion'! So much more...."

project 32
09-20-2001, 09:56 PM
so my code looks like;
imax = imax + 1
Load Sock(imax)
Sock(imax).Accept requestID
' reset the SockListen

thats in Winserv_ConnectionRequest() is that the right place? and what about dataarrival, should that now be handled by SOCK or b Winsock (i know it can be named whatever i want, but i need to know which ONE)

09-21-2001, 09:09 AM
That code looks fine.
The dataArrival event should be handled by the Winsocks that are connected. In your case it will be "Sock" because you are loading and connecting a new "Sock" on each "WinServ_ConnectionRequest()".
Test this code as your Sock_DataArrival() event to demonstrate what happens.

<pre><font color=blue>Private Sub</font color=blue> Sock_DataArrival(Index as <font color=blue>Integer</font color=blue>,<font color=blue>ByVal</font color=blue> BytesTotal as <font color=blue>Long</font color=blue>)
<font color=blue>Dim</font color=blue> sData as String
<font color=green>' get the data</font color=green>
Sock(Index).GetData sData
<font color=green>' show the data</font color=green>
Text1.Text = Index & " " & sData
<font color=blue>End Sub</font color=blue>


Hope that helps. images/icons/smile.gif


"Junior Contributor?? Argh...... ***.. ***.... I preferred 'Centurion'! So much more...."

project 32
09-23-2001, 02:48 PM
what about for Sock_SendData
how would i set it so that it sends to the server program, and all the other clients connected?

09-23-2001, 03:42 PM
I assume you mean Sock.Senddata. What you would have to do is loop through every Sock control and send the data, something lik this:
<pre><font color=blue>Dim</font color=blue> i <font color=blue>As Integer</font color=blue>

<font color=blue>For</font color=blue> i = Sock.<font color=blue>LBound</font color=blue> <font color=blue>To</font color=blue> Sock.<font color=blue>UBound</font color=blue>
<font color=blue>If</font color=blue> Sock(i).State = sckConnected <font color=blue>Then</font color=blue>
Sock(i).SendData "Hello"
<font color=blue>End If</font color=blue>
<font color=blue>Next</font color=blue> i

project 32
09-23-2001, 11:35 PM
that works, and i used it in the Sock_DataArrival part, and when i typed in Client1 i saw it in client 2, but i couldnt see client2s text in Client1

09-24-2001, 04:13 AM
Could you post you project for me to have a look at?

project 32
09-24-2001, 10:10 PM
try this out, i have added comments so maybe if you could read some of them and maybe comment back what i was doing wrong, i want to learn this stuff instead of being spoon fed thanks

the main problem right now is i need to connect 2 clients to the server and make them talk back and fourth

09-25-2001, 03:29 AM
Well for a start - some nice code there.

It works fine for me.
I changed one thing - in the cmdStop sub, the "ServerOffline" message wasn't being sent out properly to all the clients, so I changed it like so:
<pre>Private Sub cmdStop_Click() 'sends a message to everyone saying [server offline] and
'is supposto shut down the server, take a look at this to make sure it would close
'the connection properly
Dim i As Integer
For i = Sock.LBound To Sock.UBound
If Sock(i).State = sckConnected Then
Sock(i).SendData "[Server Offline]"
<font color=blue>DoEvents 'do nothing</font color=blue>
End If
Next i
End Sub

</pre>All I did was add a "DoEvents" to allow the sock to finishe sending the string before closing.

As to the other bit (yknow when the server receives a string from one client, then send it out to all the other clients), I just did the same. Add a "DoEvents" after the
Sock(i).SendData strData
and it works fine.

Keep it up man - it looks wicked!!


"Junior Contributor?? Argh...... ***.. ***.... I preferred 'Centurion'! So much more...."

project 32
09-25-2001, 08:40 AM
:P thanks alot man, u rock i didnt think i was that good of a programmer, but yeaaaah! what a boost in confidence **** thanks alot

09-25-2001, 09:33 AM
No probs man.
Seeing your code, and helping you with it has actually helped me finish off part of my tutorial on winsock!
So its a win win eh? images/icons/wink.gif


"How are we to learn, if those that know will not teach... ?" - Me.

project 32
09-25-2001, 11:03 PM
when a client disconnects, i dont think the Sock(i) on the other end is closing a connection, because if that client tries to connect again, then it cant, how can i fix this?

09-26-2001, 08:39 AM
You mean the Server side doesn't disconnect?

In that case, the client needs to send a string saying

<font color=red>"**[disconnect]**" </font color=red>

or something, and then close itself.
On the server end, you need to check the incoming data. If it contains the string, then close the sock.


Use a timer, and on the timer event, loop through the Sock array checking the state of each one.
<font color=red><pre>For i=Sock.Lbound to Sock.Ubound
if Sock(i).State &lt;&gt; sckConnected then
end if
next i
</font color=red>

If I misread, and you meant that the client Sock(i) isn't closing, then apply the same to the client side.



"How are we to learn, if those that know will not teach... ?" - Me.

project 32
10-03-2001, 10:49 PM
sometimes when my clients connect, they seemt o ahve to connect in a certian order, or else they each get an Connction error, ive only tried this with 2, but it screws up, does anyone hve any ideas before i post code?

10-04-2001, 01:03 PM
Only sometimes? Which order - the order you loaded them in?

This appears mighty odd. Unfortunately I dont have the code i downloaded ages ago, and no doubt youve changed it by now anyways....
Its probly a minor glitch or typo. Post your code if you want


"How are we to learn, if those that know will not teach... ?" - Me.

project 32
10-04-2001, 07:01 PM
well, here it is, i organized it just now and i think you should understand most of it, just keep this post alive if you dont, it foreward to my e-mail so i will know immidatly

back to the problem
im nto sure what causes it, just check my code, see if its fit, if you alter anything point it out breifly near the top of the code and then explain in detail right on the code pleeeease, i dont want to be hand fed, but im not sure how to do this

i mainly experiance it when me and my friend connect, we find that we have to connect in a certian order, and then evryone else has no problem with it

10-05-2001, 06:39 AM
Welll...... I just checked your code and to be honest it looks pretty much OK. The only thing I can see that might cause problems is that you're not unloading the Sock when a client disconnects (or am i just missing it?). If youre not, this would cause troubles with the indexing of the array.
I would suggest checking the incoming string for a signing off signal, or even in your <font color=red>tmrClose_Timer</font color=red>, whenever you close a Sock, you would have to unload it, then <font color=blue>reDim</font color=blue> the array just to make sure they're all numbered properly. I believe the statement goes like this:
<pre><font color=blue>ReDim Preserve Sock(LBound(Sock) To UBound(Sock) - 1)

</font color=blue></pre>but im not sure...

If the array isn't numbered properly, the <font color=red>Load Sock(imax)</font color=red> would cause a problem, and I _think_ would fail, but not show an error; and this would prevent your client from connecting.

Does this help? I hope so!! images/icons/smile.gif


"How are we to learn, if those that know will not teach... ?" - Me.

10-07-2001, 01:21 PM
Man that was a toughy. However, I reckon I've fixed it. If you've found a solution already project32, then dont worry, but I'm gonna post a code example in the Library just in case.
Its a development of my first chat exmaple using Winsock, which now supports multiple clients.
Its still pretty basic but worth checking out....



"How are we to learn, if those that know will not teach... ?" - Me.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum