Results 1 to 5 of 5

Thread: Winsock Question

  1. #1
    Guest

    Question

    Hi!

    Is it possible for the Winsock control to send anything else but Strings? I'd like it to send a UDT or a complete recordset of a database.... I'd like something like:

    (1)
    Type Customer
    Name as string
    ID as Long
    etc..
    end Type

    Dim TheVar as Customer
    Winsock1.SendData TheVar

    (2)
    Set TheRecordSet=TheDatabase.OpenRecordSet("Select * from blah blah")
    WinSock1.SendData TheRecordset

    Thanx very much...

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    I don't think it is possible. I am facing a similar problem, but the SendData method clearly accepts only string values. What I think I am going to do is put each value in the recordset into a string, maybe one string per record, and send the string. The string may be formatted to include the column names etc.

    This is the best we can do. Also you may have to add some encryption mechanism to protect the data while being transferred.

    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    Guest
    Is there NO way? Can't I send a UDT (User Defined Type?) Anything else?

    I suppose I could concenate all fields by a separator value (like Chr(30) or so) and then send it. On the other side I'd have to use VB6's Split function to separate the fields again.

    But my problem is I think sending strings is not the most efficient way to send all data. There must be something else... Can anyone help me please?

  4. #4
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    No way,

    At least with the Microsoft Winsock control there is no way to pass anything other than strings. You can explore other Winsock controls, but I doubt.

    What you can do is use some encryption algo to encrypt the strings you are sending. At the receiving end, decrypt them to get the strings back.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    That is not true. As long as you convert the data to one of VB's intrinsic data types it will work. I am sending records containing strings, integers, arrays of other record structures, etc. to a C server program. To do that, I first convert the record to a byte array.

    Here is the contents of 2 .frm files for a quick&dirty example client/server app that I did. You can copy the code to source files tcpclient.frm and tcpserver.frm. Pay special attention to the comments in the code regarding strings, arrays, etc. or you may have problems.

    Client source:
    Code:
    VERSION 5.00
    Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
    Begin VB.Form frmClient 
       Caption         =   "Client"
       ClientHeight    =   1410
       ClientLeft      =   60
       ClientTop       =   345
       ClientWidth     =   3075
       LinkTopic       =   "Form1"
       MaxButton       =   0   'False
       ScaleHeight     =   1410
       ScaleWidth      =   3075
       StartUpPosition =   3  'Windows Default
       Begin VB.CommandButton cmdSend 
          Caption         =   "Send"
          Height          =   375
          Left            =   2280
          TabIndex        =   4
          Top             =   600
          Width           =   735
       End
       Begin MSWinsockLib.Winsock tcpClient 
          Left            =   2520
          Top             =   0
          _ExtentX        =   741
          _ExtentY        =   741
          _Version        =   393216
       End
       Begin VB.TextBox txtPswd 
          Height          =   285
          Left            =   720
          TabIndex        =   3
          Top             =   840
          Width           =   1335
       End
       Begin VB.TextBox txtUser 
          Height          =   285
          Left            =   720
          TabIndex        =   0
          Top             =   360
          Width           =   1335
       End
       Begin VB.Label Label2 
          Caption         =   "PSWD"
          Height          =   255
          Left            =   120
          TabIndex        =   2
          Top             =   960
          Width           =   495
       End
       Begin VB.Label Label1 
          Caption         =   "USER"
          Height          =   255
          Left            =   120
          TabIndex        =   1
          Top             =   360
          Width           =   495
       End
    End
    Attribute VB_Name = "frmClient"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    ' This demo only shows how to send and receive data using the
    ' SendData and GetData methods.  The Server form does not
    ' actually perform some request from the Client.
    
    Option Explicit
    ' Use only intrinsic data types in user defined structures.
    ' DO NOT use other user defined structures, byte arrays and
    ' undimensioned strings in your user defined structure or
    ' you will have problems.
    Private Type dclLogRec
                 UserId As String * 8
                 PassWd As String * 8
            End Type
    Private Declare Sub CopyMemory Lib "KERNEL32" _
                        Alias "RtlMoveMemory" (hpvDest As Any, _
                                               hpvSource As Any, _
                                               ByVal cbCopy As Long)
    Private Sub cmdSend_Click()
       ' Store the contents of the Textboxes in the record
       ' and move it to the byte array.  Then set the variant
       ' to the byte array for the send
       
       Dim Login    As dclLogRec
       Dim bTBuff() As Byte
       Dim vTData   As Variant
    
       Login.UserId = txtUser
       Login.PassWd = txtPswd
       ReDim bTBuff(Len(Login))
       CopyMemory bTBuff(0), Login, Len(Login)
       vTData = bTBuff
       tcpClient.SendData vTData
    
    End Sub
    
    Private Sub Form_Load()
       ' The name of the Winsock control is tcpClient.
       ' Note: to specify a remote host, you can use
       ' either the IP address (ex: "121.111.1.1") or
       ' the computer's "friendly" name, as shown here.
       ' The port number must be the same as the one the Server
       ' is listening on.
       
       tcpClient.RemoteHost = "OM7635D2"
       tcpClient.RemotePort = 1001
       tcpClient.Connect
    
    End Sub
    Server source:
    Code:
    VERSION 5.00
    Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
    Begin VB.Form frmServer 
       Caption         =   "Server"
       ClientHeight    =   1230
       ClientLeft      =   60
       ClientTop       =   345
       ClientWidth     =   3495
       LinkTopic       =   "Form1"
       ScaleHeight     =   1230
       ScaleWidth      =   3495
       StartUpPosition =   3  'Windows Default
       Begin MSWinsockLib.Winsock tcpServer 
          Left            =   2520
          Top             =   360
          _ExtentX        =   741
          _ExtentY        =   741
          _Version        =   393216
       End
       Begin VB.TextBox txtPswd 
          Height          =   285
          Left            =   720
          TabIndex        =   3
          Top             =   720
          Width           =   1335
       End
       Begin VB.TextBox txtUser 
          Height          =   285
          Left            =   720
          TabIndex        =   0
          Top             =   240
          Width           =   1335
       End
       Begin VB.Label Label2 
          Caption         =   "PSWD"
          Height          =   255
          Left            =   120
          TabIndex        =   2
          Top             =   840
          Width           =   495
       End
       Begin VB.Label Label1 
          Caption         =   "USER"
          Height          =   255
          Left            =   120
          TabIndex        =   1
          Top             =   240
          Width           =   495
       End
    End
    Attribute VB_Name = "frmServer"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    ' This demo only shows how to send and receive data using the
    ' SendData and GetData methods.  The Server form does not
    ' actually perform some request from the Client.
    
    Option Explicit
    ' Use only intrinsic data types in user defined structures.
    ' DO NOT use other user defined structures, byte arrays and
    ' undimensioned strings in your user defined structure or
    ' you will have problems.
    Private Type dclLogRec
                 UserId As String * 8
                 PassWd As String * 8
            End Type
    Private Declare Sub CopyMemory Lib "KERNEL32" _
                        Alias "RtlMoveMemory" (hpvDest As Any, _
                                               hpvSource As Any, _
                                               ByVal cbCopy As Long)
    Private Sub Form_Load()
       ' Set the LocalPort property to an integer value.
       ' Then invoke the Listen method.
       
       tcpServer.LocalPort = 1001
       tcpServer.Listen
       frmClient.Show ' Show the client form.
       
    End Sub
    
    Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
       ' Check if the control's State is closed. If not,
       ' close the connection before accepting the new
       ' connection.
       
       If tcpServer.State <> sckClosed Then _
          tcpServer.Close
       ' Accept the request with the requestID
       ' parameter.
       tcpServer.Accept requestID
       
    End Sub
    
    Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
       ' Declare a byte array variable for the incoming data.
       ' Invoke the GetData method and set the Text
       ' properties to the data.
       Dim Login    As dclLogRec
       Dim bTBuff() As Byte
    
       ReDim bTBuff(bytesTotal)
       tcpServer.GetData bTBuff, vbArray + vbByte, bytesTotal
       CopyMemory Login, bTBuff(0), Len(Login)
       txtUser = Login.UserId
       txtPswd = Login.PassWd
       
    End Sub

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