Results 1 to 8 of 8

Thread: Multiple Winsocks

  1. #1

    Thread Starter
    Hyperactive Member brenaaro's Avatar
    Join Date
    Sep 2001
    Location
    Montreal, Canada
    Posts
    391

    Multiple Winsocks

    I have a question regarding multiple winsock connections between 1 server and several clients. Here is my setup:

    I have 5 winsock controls on my form:

    sckListen
    sckRequest1
    sckRequest2
    sckAccept1
    sckAccept2

    In Form_Load I do
    VB Code:
    1. With sckListen
    2. .Bind .LocalIP, 7777
    3. .Listen
    4. End With
    So now I have sckListen listening on port 7777.

    I then have 2 command buttons that I click in this order:
    VB Code:
    1. Private Sub Command2_Click()
    2.     sckRequest1.Connect sckListen.LocalIP, 7777
    3. End Sub
    4. ' and then
    5. Private Sub Command3_Click()
    6.     sckRequest2.Connect sckListen.LocalIP, 7777
    7. End Sub

    In my sckListen_ConnectionRequest I have this:
    VB Code:
    1. Private Sub sckListen_ConnectionRequest(ByVal requestID As Long)
    2.        
    3.     If sckAccept1.State = sckClosed Then
    4.         sckAccept1.Accept requestID
    5.     Else
    6.         sckAccept2.Accept requestID
    7.     End If
    8.    
    9. End Sub

    If I then check the states and ports of my winsock controls I see this:
    Code:
    Listen: Listening LocalPort:7777
    Request1: Connected LocalPort:2783
    Accept1: Connected RemotePort:2783
    Request2: Connected LocalPort:2784
    Accept2: Connected RemotePort:2784
    Does this make sense? I want to be able to have one winsock
    listening for any connection requests from clients, and it would
    then create new winsock instances to accept those requests.
    (I am familiar with loading and unloading new controls)

    Is the above log correct? Are my Request and Accept winsocks now connected on those ports? So any data passed between them should not be lost or misplaced(even though my initial .Connect method was called with port 7777)?
    And I, for one, welcome our new insect overlords. I'd like to remind them as a trusted TV personality, I can be helpful in rounding up others to toil in their underground sugar caves.

  2. #2

    Thread Starter
    Hyperactive Member brenaaro's Avatar
    Join Date
    Sep 2001
    Location
    Montreal, Canada
    Posts
    391
    I just changed my debug log to show this..
    Code:
    Listen: Listening LocalPort:7777
    Request1: Connected LocalPort:2845 RemotePort:7777
    Accept1: Connected LocalPort:7777 RemotePort:2845
    Request2: Connected LocalPort:2846 RemotePort:7777
    Accept2: Connected LocalPort:7777 RemotePort:2846
    Does this make sense?...
    And I, for one, welcome our new insect overlords. I'd like to remind them as a trusted TV personality, I can be helpful in rounding up others to toil in their underground sugar caves.

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    you can accomplish this using a control array of winsock controls.. this is the most efficient way to do it because it allows your server app to load a new winsock control with each seperate incoming connection request from the client...

  4. #4

    Thread Starter
    Hyperactive Member brenaaro's Avatar
    Join Date
    Sep 2001
    Location
    Montreal, Canada
    Posts
    391
    Yes, I had planned on using an array of winsocks. I have no concerns on that matter. What I was wondering about was the actual connections between these winsocks..the fact that it seems to be automatically assigning a new port for these incoming connection requests.

    Basically I need to know if what appears to be happening actually is. If so then that will make my life easier. Will the "Accept" winsocks automatically connect on the next available port, and will there be no conflicts between all open connections, despite the fact that the initial port (7777) seems to be being used by them all..
    And I, for one, welcome our new insect overlords. I'd like to remind them as a trusted TV personality, I can be helpful in rounding up others to toil in their underground sugar caves.

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Each instance of the winsock control has its own index even if they share the same port number. You'll probably have problems with 'address in use' errors since each remote IP can only be accessed with 1 winsock whatever its state if its remoteIP property already has a value. You can avoid that by unloading the instance after use or designating each instance to 1 remote IP address (no other instance can access same address). Side note, you wont be able to have three simultaneous winsocks to same remotehostIP.

  6. #6
    Addicted Member TheSarlacc's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere close
    Posts
    185
    i think u want this code: (stole it from MSDN)..

    Code:
    Const MAX_CONNECTIONS =100
    
    Private Sub Form_Load()
       sckServer(0).LocalPort = 7777
       sckServer(0).Listen
       'load all winsock controls
       For indx = 1 to MAX_CONNECTIONS
            Load sckServer(indx)
       Next indx
    End Sub
    
    Private Sub sckServer_ConnectionRequest _
    (Index As Integer, ByVal requestID As Long)
       If Index = 0 Then
          'find available winsock control
          For indx = 1 to MAX_CONNECTIONS
                If sckServer(indx).State <> sckConnected Then
                    'accept connection
                    sckServer(indx).Close
                    sckServer(indx).Accept requestID
                    Exit For
                End If
          Next indx
       End If
    End Sub
    Get Quoted:

    "If Shane Warne had given his mobile phone to Russell Crowe, neither of them would be in trouble."
    "She has more troubles than a centipede with its legs crossed."
    "For every action, there is an equal and opposite Government program."


  7. #7
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    brenaaro,

    Search the forum for Winsock.

    This question has been asked and answered several times.

  8. #8

    Thread Starter
    Hyperactive Member brenaaro's Avatar
    Join Date
    Sep 2001
    Location
    Montreal, Canada
    Posts
    391
    Originally posted by TheSarlacc
    i think u want this code: (stole it from MSDN)..

    Code:
    Const MAX_CONNECTIONS =100
    
    Private Sub Form_Load()
       sckServer(0).LocalPort = 7777
       sckServer(0).Listen
       'load all winsock controls
       For indx = 1 to MAX_CONNECTIONS
            Load sckServer(indx)
       Next indx
    End Sub
    
    Private Sub sckServer_ConnectionRequest _
    (Index As Integer, ByVal requestID As Long)
       If Index = 0 Then
          'find available winsock control
          For indx = 1 to MAX_CONNECTIONS
                If sckServer(indx).State <> sckConnected Then
                    'accept connection
                    sckServer(indx).Close
                    sckServer(indx).Accept requestID
                    Exit For
                End If
          Next indx
       End If
    End Sub
    Ok I do something like this except I dont load the other winsocks untill they are needed. This is what I wanted to know, as long as the same concept is in MSDN then I'm satisfied.
    And I, for one, welcome our new insect overlords. I'd like to remind them as a trusted TV personality, I can be helpful in rounding up others to toil in their underground sugar caves.

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