register.dani@gmail.com

Multi TCP clien connection with unisock (Uniscok array) [VB]

About Unisock      Unisock's A new class implementation of Winsock API. By style close to the native Winsock control and other class implementations such as CSocket and CSocketMaster, so you don't need to learn or rewrite much of existing code. The new and cool part about this class is that it is just one class file. Also, it performs better (by speed) and handles errors a bit more cleanly (you aren't forced to close the socket each time an error occurs). Other speciality is transparent Unicode support: when you switch to text mode, you start receiving TextArrival event instead of DataArrival and start getting individual lines. These lines are automatically Unicode if received line is UTF-8 or UTF-16! ANSI lines require you to use StrConv to get an usable string, thus you have the power on what to do with the raw data before any conversion has affected it.

By: Vesa Piittinen aka Merri, http://vesa.piittinen.name/
vesa@piittinen.name
LICENSE:
http://creativecommons.org/licenses/by-sa/1.0/fi/deed.en
--------------------------------------------------------------------------------

   I think unisock's A  good Winsock API, but problem's found when I need usage multi connection with array, unisock's not supported for that :D. But with "user control" (ctl) we can usage unisock for multi array.

 Lets Do....
1. Load unisock class to vb project

2. Create/Add new user control






I give "UniControl" as name for user control, and note this code at there (at user control) :


'*/-----------------------------------START------------------------------------/*
  Private WithEvents Client As UniSock
  Public Event CloseSck()
  Public Event Connect()
  Public Event DataArrival(ByVal BytesTotal As Long)
  Public Event 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)
  Private Sub Client_Closing()
      RaiseEvent CloseSck
  End Sub
  Private Sub Client_Connect()
      RaiseEvent Connect
  End Sub
  Private Sub Client_DataArrival(ByVal BytesTotal As Long)
      RaiseEvent DataArrival(BytesTotal)
  End Sub
  Public Function GetData() As String
      Dim strData As String
      Client.GetData strData
      GetData = strData
  End Function
  Private Sub Client_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)
      RaiseEvent Error(Number, Description, sCode, Source, HelpFile, HelpContext, CancelDisplay)
  End Sub
  Public Sub SetSock()
      Set Client = New UniSock
  End Sub
  Public Sub Connect(serverCon As String, portCon As Long)
      Client.Connect serverCon, portCon
  End Sub
  Public Sub CloseSck()
      Client.CloseSocket
  End Sub
  Public Sub SendData(datnya)
      Client.SendData datnya
  End Sub
'*/-----------------------------------END-----------------------------------/*


3. Save project, close and reopen (Without close, new user control can be usage)
4.Pleace unicontrol to form
 (There I give 3 unicontrol (3 connection) as array, with array name's UniControl(0) - UniControl(2) )
- add new button, I give that button name's "cmdConnect"
- add new textbox with "Text1" as name

5.Now, right click at form and view code, and note this :

'---------------------- START ----------------------
'--- This code for activate unisock with user control
Private Sub Form_Load()
Dim N As Integer
For N = 0 To 2   ' <---  2 because Am only add 3 array at unicontrol (0,1,2)
    UniControl(N).SetSock
Next N
End Sub

'--- This code for button (Manage connect to site)
Private Sub cmdConnect_Click()
Dim N As Integer
For N = 0 To 2
  UniControl(N).CloseSck
  UniControl(N).Connect "dani-diam.blogspot.com", 80
Next N
End Sub

'---This code for some action when unisock's closed
Private Sub UniControl_CloseSck(c As Integer)
    Text1.SelText = c & ":Connection CLosed" & vbCrLf <- Report to text1 about sock condition
    UniControl(c).CloseSck
End Sub

'--- This code for some action when unisock's error
Private Sub UniControl_Error(c As Integer, 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.SelText = c & ": Error Connection" & vbCrLf
    UniControl(c).CloseSck
End Sub

'--- This code for some action when unisock's connected
Private Sub UniControl_Connect(c As Integer)
    Text1.SelText = c & ":Connected" & vbCrLf
    UniControl(c).SendData urlData '<- Sending UrlData packet
End Sub

'--- This code for urlData
Private Function urlData() As String
urlData = "GET / HTTP/1.1" & vbCrLf & _
    "Host: " & "dani-diam.blogspot.com" & vbCrLf & _
    "Connection: Keep-Alive" & _
     vbCrLf & vbCrLf
End Function
'---------------------- END ----------------------

Unisock you can download here

3 comments: