Results 1 to 16 of 16

Thread: Winsock with multi-users

  1. #1
    Stiletto
    Guest

    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

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    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:
    1. Private intMax As Long
    2.  
    3. Private Sub Form_Load()
    4.    intMax = 0
    5.    sckServer(0).LocalPort = 1001
    6.    sckServer(0).Listen
    7. End Sub
    8.  
    9. Private Sub sckServer_ConnectionRequest _
    10. (Index As Integer, ByVal requestID As Long)
    11.    If Index = 0 Then
    12.       intMax = intMax + 1
    13.       Load sckServer(intMax)
    14.       sckServer(intMax).LocalPort = 0
    15.       sckServer(intMax).Accept requestID
    16.       Load txtData(intMax)
    17.    End If
    18. End Sub

  3. #3
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746
    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?

  4. #4
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746
    and what does the winsock bind action do?

  5. #5
    Stiletto
    Guest
    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?

  6. #6
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    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

  7. #7
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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.
    Baaaaaaaaah

  8. #8
    Stiletto
    Guest
    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?

  9. #9
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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.
    Baaaaaaaaah

  10. #10
    Stiletto
    Guest
    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..)

  11. #11
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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:
    1. For i = 1 To 20
    2. Load Winsock1(i)
    3. 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:
    1. Winsock1(0).Listen  'this winsock control listens on a specific port
    2. Winsock1(1).Connect blahblah 'this is a totally different winsock
    3.                                                  'control.  it'll connect to a specific
    4.                                                  'computer without interrupting the
    5.                                                  'above winsock.
    Baaaaaaaaah

  12. #12
    Stiletto
    Guest
    k tnx i'll try to work this thing out

  13. #13
    Stiletto
    Guest
    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?

  14. #14
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746
    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.

  15. #15
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746
    whats the difference between winsock API and using the winsock control?

  16. #16
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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.
    Baaaaaaaaah

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width