Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Communications > multiple clients


Reply
 
Thread Tools Display Modes
  #1  
Old 09-18-2001, 12:11 AM
project 32
Guest
 
Posts: n/a
Default multiple clients


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?

Reply With Quote
  #2  
Old 09-18-2001, 02:12 AM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default Re: multiple clients

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
SockListen.Close
SockListen.Listen
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
Sock(0)
Sock(1)
Sock(2)
Sock(3)
etc... the way any other array is numbered.

Hope this helps

Chief

"Junior Contributor?? Argh...... ***.. ***.... I preferred 'Centurion'! So much more...."
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #3  
Old 09-19-2001, 08:38 PM
project 32
Guest
 
Posts: n/a
Default Re: multiple clients

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)?

Reply With Quote
  #4  
Old 09-20-2001, 12:10 PM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default Re: multiple clients

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.)

Chief

"Junior Contributor?? Argh...... ***.. ***.... I preferred 'Centurion'! So much more...."
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #5  
Old 09-20-2001, 09:56 PM
project 32
Guest
 
Posts: n/a
Default Re: multiple clients

so my code looks like;
imax = imax + 1
Load Sock(imax)
Sock(imax).Accept requestID
' reset the SockListen
WinServ.Close
WinServ.Listen

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)

Reply With Quote
  #6  
Old 09-21-2001, 09:09 AM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default Re: multiple clients

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>

</pre>

Hope that helps. [img]images/icons/smile.gif[/img]

Chief

"Junior Contributor?? Argh...... ***.. ***.... I preferred 'Centurion'! So much more...."
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #7  
Old 09-23-2001, 02:48 PM
project 32
Guest
 
Posts: n/a
Default Re: multiple clients

what about for Sock_SendData
how would i set it so that it sends to the server program, and all the other clients connected?

Reply With Quote
  #8  
Old 09-23-2001, 03:42 PM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default Re: multiple clients

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

__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #9  
Old 09-23-2001, 11:35 PM
project 32
Guest
 
Posts: n/a
Default Re: multiple clients

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

Reply With Quote
  #10  
Old 09-24-2001, 04:13 AM
Banjo's Avatar
Banjo Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default Re: multiple clients

Could you post you project for me to have a look at?

__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #11  
Old 09-24-2001, 10:10 PM
project 32
Guest
 
Posts: n/a
Default Re: multiple clients

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

Reply With Quote
  #12  
Old 09-25-2001, 03:29 AM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default Re: multiple clients

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>
Sock(i).Close
WinServ.Close
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!!

Chief

"Junior Contributor?? Argh...... ***.. ***.... I preferred 'Centurion'! So much more...."
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #13  
Old 09-25-2001, 08:40 AM
project 32
Guest
 
Posts: n/a
Default Re: multiple clients

: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

Reply With Quote
  #14  
Old 09-25-2001, 09:33 AM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default Re: multiple clients

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? [img]images/icons/wink.gif[/img]

Chief

"How are we to learn, if those that know will not teach... ?" - Me.
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #15  
Old 09-25-2001, 11:03 PM
project 32
Guest
 
Posts: n/a
Default Re: multiple clients

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?

Reply With Quote
  #16  
Old 09-26-2001, 08:39 AM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default Re: multiple clients

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.

OR

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
Sock(i).Close
end if
next i
</font color=red>
</pre>

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

[img]images/icons/wink.gif[/img]

Chief

"How are we to learn, if those that know will not teach... ?" - Me.
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #17  
Old 10-03-2001, 10:49 PM
project 32
Guest
 
Posts: n/a
Default Re: multiple clients

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?

Reply With Quote
  #18  
Old 10-04-2001, 01:03 PM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default Re: multiple clients

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

Chief

"How are we to learn, if those that know will not teach... ?" - Me.
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #19  
Old 10-04-2001, 07:01 PM
project 32
Guest
 
Posts: n/a
Default Re: multiple clients

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

Reply With Quote
  #20  
Old 10-05-2001, 06:39 AM
ChiefRedBull's Avatar
ChiefRedBull ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default Re: multiple clients

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!! [img]images/icons/smile.gif[/img]

Chief

"How are we to learn, if those that know will not teach... ?" - Me.
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->