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?
 
taskyConnect With a Mentor Commented:

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
 
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:
Did that work?
0
All Courses

From novice to tech pro — start learning today.