VB Multithread socket server

Hi masters,

I need the code  in VB 6.0 to make a Multithread socket server, its this posible?.

Regards
kidmaverickAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

taskyCommented:
Sure. Place two Winsock controls on your form. Name one "sckListen" and the other "sckClient". Set the index of sckClient to 0 (create a control array). sckListen is the listening socket and handles all incoming connections. When a request is made, it assigns a sckClient to handle the connection. If you need more explanation let me know.
Private Sub StartServer(intPort As Integer)
    'Start listening
    sckListen.Close
    sckListen.LocalPort = intPort
    sckListen.Listen
End Sub
 
Private Sub sckListen_ConnectionRequest(ByVal requestID As Long)
    Dim I As Integer
    Dim bFlag As Boolean
    'We have a connection request
    'Loop through all clients and see if we have any disconnected
    For I = 0 To sckClient.UBound
        If sckClient(I).State = sckClosed Then
            bFlag = True
            Call sckClient(I).Accept(requestID)
            Exit For
        End If
    Next
    'If the flag wasn't thrown, we need to create a new socket to accept
    If Not bFlag Then
        Load sckClient(sckClient.UBound + 1)
        Call sckClient(sckClient.UBound).Accept(requestID)
    End If
End Sub

Open in new window

0
kidmaverickAuthor Commented:
Tasky,

This is my dummy and change the code to function as what you provided.

Regards
Option Explicit
Dim iSockets As Integer
Dim sRequestID As String
 
Private Sub Form_Load()    
    Socket(0).LocalPort = 8888
    Socket(0).Listen
End Sub
 
Private Sub socket_Close(Index As Integer)
    Socket(Index).Close
    Unload Socket(Index)
    iSockets = iSockets - 1   
End Sub
 
Private Sub socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    
  If Index = 0 Then
    sRequestID = requestID
    iSockets = iSockets + 1
    Load Socket(iSockets)    
    Socket(iSockets).LocalPort = 1007
    Socket(iSockets).Accept requestID
  End If
 
End Sub
 
Private Sub socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
 
    Dim PBRequestXML As String	 
 
    ' Get data from client
    Socket(Index).GetData PBRequestXML, vbString
    Socket(Index).GetData Buffer
    
 
    Socket(Index).SendData " -- " & PBRequestXML
 
End Sub

Open in new window

0
taskyCommented:

Option Explicit
Dim iSockets As Integer
Dim sRequestID As String
 
Private Sub Form_Load()    
    Socket(0).LocalPort = 8888
    Socket(0).Listen
End Sub
 
Private Sub socket_Close(Index As Integer)
    Socket(Index).Close
    'Unload Socket(Index)
    'iSockets = iSockets - 1   
    ' Removed this because we don't always know if the last socket was removed, and we can just recycle sockets (see below)
End Sub
 
Private Sub socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
  Dim I As Integer  
  If Index = 0 Then
    sRequestID = requestID
    'This will recycle old sockets. Loops through and finds a socket
    'that is disconnected. If successful, that socket takes the connection.
    'Otherwise, we load a new socket.
    For I = 1 To Socket.UBound
      If Socket(I).State = sckConnected Then
        Socket(I).Accept requestID
        Exit Sub
      End If
    Next
    Load Socket(Socket.UBound + 1)
    Socket(Socket.UBound).Accept requestID
  End If
 
End Sub
 
Private Sub socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
 
    Dim PBRequestXML As String   
 
    ' Get data from client
    Socket(Index).GetData PBRequestXML, vbString
    'No need to grab the data twice!?
 
    Socket(Index).SendData " -- " & PBRequestXML
 
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
taskyCommented:
Did that work?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.