Sample code for 30+ languages & platforms
DataFlex

Send a 16-bit Integer over a Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.SendInt16, which sends the low 16 bits of a value as a two-byte integer in the selected byte order.

Background. Fixed-size integers and length prefixes are common building blocks for application-defined binary protocols. Byte order is selectable, with big-endian being network byte order.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSocket
    Boolean iBTls
    Integer iMaxWaitMs
    Boolean iBBigEndian
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatSocket)) To hoSocket
    If (Not(IsComObjectCreated(hoSocket))) Begin
        Send CreateComObject of hoSocket
    End

    //  Connect to the server using TLS.
    Move True To iBTls
    Move 5000 To iMaxWaitMs
    Get ComConnect Of hoSocket "example.com" 5000 iBTls iMaxWaitMs To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Send the low 16 bits of the value as a two-byte integer.  Use True for network byte order
    //  (big-endian) or False for little-endian.
    Move True To iBBigEndian
    Get ComSendInt16 Of hoSocket 1024 iBBigEndian To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "16-bit integer sent."


End_Procedure