Results 1 to 2 of 2

Thread: Using Winsock to send and get User Defined Types

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Location
    France
    Posts
    4

    Question

    My question goes as follows :

    I am trying to "talk" to an SMS server through its TCP port. This little guy sends packets with defined structures.
    I have enough documentation to recreate these structures in VB but I cant figure out how to have the Winsock Control receive such packets.

    Let me give you an example :

    Code:
    Public Type Header_data
        Length As Integer
        CorrelationNumber As Integer
        MessageType As Byte
        Operation As Byte
        Unused As Integer
    End Type
    I would like to do something like :

    Code:
    Dim Header as Header_Data
    
    '... (whatever)....'
    
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
        Winsock1.GetData Header
    End Sub
    But it seems that Winsock does not accept User Defined Type as parameter...

    Is there a way to do this ???




    [Edited by Coolhp on 08-20-2000 at 05:41 PM]

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Location
    France
    Posts
    4

    Red face

    Ok... Here is the solution guys.

    SUMMARY
    The Winsock control methods SendData and GetData send and receive data through an argument declared of type variant. The following variant types are supported:

    Byte vbByte
    Integer vbInteger
    Long vbLong
    Single vbSingle
    Double vbDouble
    Currency vbCurrency
    Date vbDate
    Boolean vbBoolean
    SCODE vbError
    String vbString
    Byte Array vbArray + vbByte

    Because user-defined types are not directly supported, you must make use of the byte-array data type in order to pass a user-defined type with the Winsock control. This article demonstrates how to send and receive data contained in a user-defined type with the Winsock Control.


    MORE INFORMATION
    The example uses the Winsock TCP client and server created in the following Microsoft Knowledge Base article:

    Q152057 HOWTO: Create & Use a Client/Server Using Winsock TCP Controls

    Prior to following this example, make sure you have at least built the framework for the client and server discussed in the aforementioned article. You will be required to make modifications to the DataArrival Event subroutine and the SendData Command subroutine.

    Step by Step Example

    Enter the following code in the General declarations section of Form1 of both the client and server:

    Code:
    Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" ( hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    
       Private Type MyType
          i As Integer
          l As Long
          s As String * 8
          d As Double
       End Type

    Enter the following code for the DataArrival Event subroutine in the Winsock server:

    Code:
    Private Sub TCP1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
             Dim vta
             Dim mt As MyType
             Dim bt() As Byte
             TCP1(Index).GetData vta, vbArray + vbByte, LenB(mt)
             bt = vta
             CopyMemory mt, bt(0),  LenB(mt)
             Debug.Print mt.i; ","; mt.l; ","; mt.s; ","; mt.d
          End Sub

    Enter the following code for the SendData Command button in the Winsock client:

    Code:
       Private Sub cmdSendData_Click()
             Dim mt As MyType
             mt.i = 99
             mt.l = 33
             mt.s = "abcd"
             mt.d = 23.76
             Dim bt() As Byte
             ReDim bt(LenB(mt)) as Byte
             Dim vtdata As Variant
             CopyMemory bt(0), mt, LenB(mt)
             vtdata = bt
             TCP1.SendData vtdata
          End Sub
    Courtesy to Microsoft Knowledge Base.

    Hope this helps...

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