is there any way to use a struct when using tcp socket network programming? or is byte[] my only option? RRS feed

  • Question

  • I have a basic confusion between serial port and network TCP communication to devices.

    My colleague uses a vb struct as the nice easy to read way to create a packet to send out via a serial port. 
    Like this basic code sample below of his serial call followed at bottom by my c# tcp beginsend network call.   
    Can I use a struct as well to call out for my TCP network beginsend? 
    By looking at the signature of BeginSend(Byte[] as the beginning of the signature for that method call to send data to a connected socket, I don't see myself being able to use a struct.
    Thank you for any clarification/explanation. 

    Public Structure PacketRecord
        Dim SourceAddress As Integer
        Dim DestinationAddress As Integer
        Dim Length As Integer
        Dim Ftype As String
        Dim Pdu As String
        Dim Valid As Boolean
        Dim Status As String
    End Structure
    txp = buildpacket(txpacket)
        Public Function buildpacket(ByVal packet As PacketRecord) As String
            Dim temppacket As String
            Dim checksum, i As Integer
            'Build packet to Transmit
            'Add Header & SFD
            temppacket = Chr(255) & Chr(255) & Chr(255) & Chr(255) & Chr(254)
            'Add Destination *& Source Addresses
            temppacket = temppacket & Chr(packet.DestinationAddress) & Chr(packet.SourceAddress)
            'Add the packet length
            temppacket = temppacket & Chr((Len(packet.Pdu) + 8) / 256)
            temppacket = temppacket & Chr((Len(packet.Pdu) + 8) Mod 256)
            'Add the F type
            temppacket = temppacket & Chr(Int(Val("&H" & Mid(packet.Ftype, 1, 2)))) 'Left(ftype, 2))))
            temppacket = temppacket & Chr(Int(Val("&H" & Mid(packet.Ftype, 3, 2)))) 'Right(ftype, 2))))
            'Add the PDU
            temppacket = temppacket & packet.Pdu
            'Calculate Checksum
            checksum = 0
            For i = 6 To Len(temppacket)
                checksum = checksum + Asc(Mid(temppacket, i, i))
            Next i
            checksum = checksum Mod 65536
            checksum = 65535 - checksum
            'Add the Checksum
            temppacket = temppacket & Chr(checksum \ 256)
            temppacket = temppacket & Chr(checksum Mod 256)
            buildpacket = temppacket
            'Tabel5.Text = mode
        End Function
    vs what I have to do with byte arrays with my TCP BeginSend code.  Can I use structs or what am I getting wrong here? Thank you for any clarification;

    toSocket = new byte[7];
    toSocket[0] = 2; //msgID
    toSocket[1] = 0; //status
    toSocket[2] = 0; //data lehgth
    toSocket[3] = 0;
    toSocket[4] = 0; //seq #
    toSocket[5] = 1;
    client.BeginSend(toSocket, 0, toSocket.Length, SocketFlags.None,
                      new AsyncCallback(SendCallback), client)
    Monday, December 7, 2009 7:19 PM

All replies

  • That VB structure, PacketRecord, is at the upper limit of practical size. 
    If you are using and creating a lot of these, you may with to consider the type StringBuilder instead of type String.

    Can you use a structure with C#?  Of course, although I think a class might work better.
    You just cannot pass the structure into the method.
    But, you could write a method that accepted your structure as a parameter, packed it into an array and called BeginSend.

    Are you familiar with "object wrappers" in Object Oriented Programming .?
    Basically, the idea is to take an object and define it within a class in such a way that it is easier to use.
    Sort of like putting handles onto something so that you can grab with a fork lift.
    They are called wrappers because you no longer see the original object, just the wrapper class.

        abstract class Animal
        class Cat : Animal
        class Dog : Animal
        class Hamster : Animal
        class HousePet
            Animal pet;
            public HousePet(Animal pet)
       = pet;

    The class HousePet is a wrapper for classes derived from animals.  Why do this?  This separates the behavior and characteristics of an Animal from HousePet.  A pet could have a name, homecoming date. birth date, favorite food, etc.  Animals might not a homecoming date, but they would have a birth date.  Wrappers can selectively expose members of the wrapped up class, and even add/modify the functionality of those members.

    This example shows an Animal being wrapped by HousePet, one type wrapped up inside of another.  What if you need to wrap functionality?  Some complex functionality.  That is where Layers and Tiers come in?  They separate the logistics of how things work from clients.  In your case, this could mean creating a separate class or assembly, which provided methods that you call to perform the communication with any type of port.  You just simply call the proper class for the type of port, and call methods that accept your structure as a parameter.

    Hope this helps.

    Rudy  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by hazz Monday, December 7, 2009 8:15 PM
    • Unmarked as answer by hazz Monday, December 7, 2009 8:15 PM
    Monday, December 7, 2009 8:09 PM
  • Hi hazz,

    If you still can't solve it, can you please provide newest result about this question?

    Best regards,
    Guang-Ming Bian - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact
    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Friday, December 11, 2009 6:04 AM