|
-
Nov 15th, 2001, 05:13 PM
#1
Winsock with multi-users
sups,
I'm working on a lil chat program. I cant connect 2 remote users to the same server, so how can i make winsock to work with multi users?
tnx
-
Nov 15th, 2001, 05:19 PM
#2
PowerPoster
Accepting More than One Connection Request
The basic server outlined above accepts only one connection request. However, it is possible to accept several connection requests using the same control by creating a control array. In that case, you do not need to close the connection, but simply create a new instance of the control (by setting its Index property), and invoking the Accept method on the new instance.
The code below assumes there is a Winsock control on a form named sckServer, and that its Index property has been set to 0; thus the control is part of a control array. In the Declarations section, a module-level variable intMax is declared. In the form's Load event, intMax is set to 0, and the LocalPort property for the first control in the array is set to 1001. Then the Listen method is invoked on the control, making it the "listening control. As each connection request arrives, the code tests to see if the Index is 0 (the value of the "listening" control). If so, the listening control increments intMax, and uses that number to create a new control instance. The new control instance is then used to accept the connection request.
VB Code:
Private intMax As Long
Private Sub Form_Load()
intMax = 0
sckServer(0).LocalPort = 1001
sckServer(0).Listen
End Sub
Private Sub sckServer_ConnectionRequest _
(Index As Integer, ByVal requestID As Long)
If Index = 0 Then
intMax = intMax + 1
Load sckServer(intMax)
sckServer(intMax).LocalPort = 0
sckServer(intMax).Accept requestID
Load txtData(intMax)
End If
End Sub
-
Nov 15th, 2001, 05:24 PM
#3
Fanatic Member
a few questions.
1. how is it possible to have more then one winsock listening on the same port and accept multiple connections within a app but now have more then one listening on the same port if they are in 2 different apps??
2. do all major apps use winsock? for example does internet explorer use winsock or some other advanced thing?
3. using the integer you can only load a mere 10000 winsocks. is there any way that you can load more? maybe by using a different variable?
-
Nov 15th, 2001, 05:24 PM
#4
Fanatic Member
and what does the winsock bind action do?
-
Nov 15th, 2001, 05:31 PM
#5
sry chrisjk, but i dont understand...
I got 1 server (The one who's listening) and 2 clients.
I can connect 1 client to the server but then i cant connect the other.
How can i connect more then 1 client to the same server?
-
Nov 15th, 2001, 05:45 PM
#6
PowerPoster
Originally posted by flamewavetech
a few questions.
1. how is it possible to have more then one winsock listening on the same port and accept multiple connections within a app but now have more then one listening on the same port if they are in 2 different apps??
2. do all major apps use winsock? for example does internet explorer use winsock or some other advanced thing?
3. using the integer you can only load a mere 10000 winsocks. is there any way that you can load more? maybe by using a different variable?
1) Probably not, you'll get "address in use" I expect.
2) Hell no. They use winsock API's, not the control
3) Use a long??
and what does the winsock bind action do?
It's used for UDP connections to join the server and client winsocks together so they can communicate. Not used in TCP
sry chrisjk, but i dont understand...
I can't really explain it better than the quote I already gave. Just follow the instructions and copy/paste the code example
-
Nov 15th, 2001, 05:59 PM
#7
PowerPoster
I was also working on a multi-user chat program as a project, but I stopped for some reason.
One winsock control listens on a specific port, and when it recieves a connection request or the user, himself, wants to connect to another computer, it loads another winsock and uses that to make a new connection.
Check out the attached project. It's is a little messy and buggy. Don't try to send any file 'cause it'll give you some error message.
Just look at the basic code that was used to be able to host more than 1 user.
-
Nov 15th, 2001, 06:00 PM
#8
I think i'm gettign it man...
But:
1. y using the Load command?
2. How do i know from the start how many control arrays i need to create?
-
Nov 15th, 2001, 06:04 PM
#9
PowerPoster
Originally posted by Stiletto
I think i'm gettign it man...
But:
1. y using the Load command?
2. How do i know from the start how many control arrays i need to create?
1. Load command is used to Load an object. If you have any array of winsocks then you will be able to use each one of them to create a different connection to the remote PC.
2. You don't need to know how many control arrays you want. If you have some user limitation then, let us say the user limitation is 20, you can just load 20 winsocks when your form loads, and after than, you use the next non-busy winsock in the array to create a new connection with the other computer.
-
Nov 15th, 2001, 06:11 PM
#10
Originally posted by abdul
2. You don't need to know how many control arrays you want. If you have some user limitation then, let us say the user limitation is 20, you can just load 20 winsocks when your form loads, and after than, you use the next non-busy winsock in the array to create a new connection with the other computer.
Do i need to add the Winsock control to the project as many times as i need (guess not). If not, how do i declare the winsock to b an array? (write me an xmaple of declaring such a thinik..)
-
Nov 15th, 2001, 06:18 PM
#11
PowerPoster
You just need to add 1 winsock control on the form. You create more winsocks out of it at runtime.
Add a winsock control on the form and set its index to 0.
Now, you use the following code:
VB Code:
For i = 1 To 20
Load Winsock1(i)
Next
It'll load 20 different winsock controls. Now, you have 21 winsocks that you can work with.
The only difference is that you use the winsock by referring to its index like this:
VB Code:
Winsock1(0).Listen 'this winsock control listens on a specific port
Winsock1(1).Connect blahblah 'this is a totally different winsock
'control. it'll connect to a specific
'computer without interrupting the
'above winsock.
-
Nov 15th, 2001, 06:38 PM
#12
k tnx i'll try to work this thing out
-
Nov 15th, 2001, 08:01 PM
#13
I'm back 
k, Everything is working fine...
Now i got 1 client connected to the server (Client1 is a project and the Server is a different project)
The server uses port 1020 (LocalPort = 1020)
Now i want another client to join the "chat", but i dont know which port to set to connect cuz port 1020 is in use...How can i know which port to set?
-
Nov 15th, 2001, 09:08 PM
#14
Fanatic Member
if u have a indexed winsock listining on the same port just keep the winsock(0) listining and have all the other ones accept like winsock(1) then winsock(2) winsock (3) etc. that way u can have as many winsocks connect as u want to the same port. so have them all connect to port 1020 or whatever winsock(0) is listining too.
-
Nov 15th, 2001, 09:08 PM
#15
Fanatic Member
whats the difference between winsock API and using the winsock control?
-
Nov 15th, 2001, 10:29 PM
#16
PowerPoster
Originally posted by flamewavetech
whats the difference between winsock API and using the winsock control?
Winsock control is made up of Winsock API. If you are using C++ or other programming language, you usually use Winsock API.
People who have the Learning Edition of vb can also use a "Winsock API module" that declares all the API calls. It functions the same way as Winsock Control does, but Winsock Control is a lot easier than API.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|