frmClient.frm

 VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form frmClient
   Caption         =   "Client"
   ClientHeight    =   3375
   ClientLeft      =   8460
   ClientTop       =   3960
   ClientWidth     =   5880
   LinkTopic       =   "Form1"
   ScaleHeight     =   3375
   ScaleWidth      =   5880
   Begin VB.CommandButton cmdSend
      Caption         =   "Send Data"
      Height          =   360
      Left            =   1950
      TabIndex        =   2
      Top             =   2940
      Width           =   1710
   End
   Begin MSWinsockLib.Winsock TCPClient3
      Left            =   5430
      Top             =   2910
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
   End
   Begin MSWinsockLib.Winsock TCPClient2
      Left            =   4080
      Top             =   2940
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
   End
   Begin VB.TextBox Text1
      Height          =   2775
      Left            =   75
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   1
      Top             =   90
      Width           =   5730
   End
   Begin VB.CommandButton btnConnect
      Caption         =   "Connect to Server"
      Height          =   360
      Left            =   75
      TabIndex        =   0
      Top             =   2940
      Width           =   1710
   End
   Begin MSWinsockLib.Winsock TCPClient1
      Left            =   4785
      Top             =   2925
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
   End
End
Attribute VB_Name = "frmClient"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub btnConnect_Click()
  Text1.Text = Text1.Text & "Connecting to remote host" & vbCrLf
 
  With TCPClient1
    .Close
    .RemoteHost = .LocalHostName
    .RemotePort = 9999
    .Connect
  End With
  With TCPClient2
    .Close
    .RemoteHost = .LocalHostName
    .RemotePort = 9999
    .Connect
  End With
  With TCPClient3
    .Close
    .RemoteHost = .LocalHostName
    .RemotePort = 9999
    .Connect
  End With

End Sub

'---------------------------------------------------------------------------------------
' Procedure : Sub cmdSend_Click
'           : Send Messages to the server by 3 different Winsock Controls
'---------------------------------------------------------------------------------------
Private Sub cmdSend_Click()
  Dim oStream As TCPManagedStream.Stream
 
  Dim bArr() As Byte
  Dim x As Long
  Dim y As Long
 
  Set oStream = New Stream
 
  Call oStream.Send(TCPClient2, , AsByteArray, 6)
  Call oStream.Send(TCPClient3, , AsString, 6)
 
  Call oStream.Send(TCPClient1, "Hello", AsString)
  Call oStream.Send(TCPClient1, "how", AsString)
  Call oStream.Send(TCPClient2, "are", AsString)
  Call oStream.Send(TCPClient3, "you", AsString)

  ReDim bArr(25)

  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  ''' using the optional identifier argument to identify the type of message
  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  For x = 0 To UBound(bArr)
    bArr(x) = 65 + x          ''' Send all Uppercase Letters
  Next
  Call oStream.Send(TCPClient1, bArr, AsByteArray, 1)

  For x = 0 To UBound(bArr)
    bArr(x) = 97 + x          ''' Send all Lowercase Letters
  Next
  Call oStream.Send(TCPClient3, bArr, AsByteArray, 2) ''' using the optional identifier argument

  Call oStream.Send(TCPClient1, "Sending alphabet 100 times", AsString)
  For y = 1 To 100
    Call oStream.Send(TCPClient3, bArr, AsByteArray)
  Next


  ''' NOTE :
  ''' When the server receives the data it will not be in the same order as it was sent
  ''' ie. all TCPClient1 Data will be received first, TCPClient2 next etc.
  ''' this is because the small messages for each winsock control are packed together into one stream
  ''' but the individual messages will be received separetely

  Set oStream = Nothing
End Sub

Private Sub TCPClient1_Connect()
  Text1.Text = Text1.Text & "Winsock1 Connected" & vbCrLf
End Sub

Private Sub TCPClient2_Connect()
  Text1.Text = Text1.Text & "Winsock2 Connected" & vbCrLf
End Sub

Private Sub TCPClient3_Connect()
  Text1.Text = Text1.Text & "Winsock3 Connected" & vbCrLf
End Sub

Private Sub TCPClient1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
  Text1.Text = Text1.Text & Description & vbCrLf
End Sub

Private Sub TCPClient2_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
  Text1.Text = Text1.Text & Description & vbCrLf
End Sub

Private Sub TCPClient3_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
  Text1.Text = Text1.Text & Description & vbCrLf
End Sub

Project Homepage: