Sample code for 30+ languages & platforms
DataFlex

Receive a 16-bit Integer from a Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveInt16, which reads exactly two bytes as a 16-bit integer and stores the result in the ReceivedInt property. Byte order and signedness are selectable.

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
    Boolean iBUnsigned
    String sTemp1
    Integer iTemp1

    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

    //  Read exactly two bytes as a 16-bit integer and store the result in the ReceivedInt property.
    //  The 1st argument selects byte order (True = big-endian); the 2nd selects unsigned interpretation.
    Move True To iBBigEndian
    Move False To iBUnsigned
    Get ComReceiveInt16 Of hoSocket iBBigEndian iBUnsigned To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComReceivedInt Of hoSocket To iTemp1
    Showln "Received 16-bit integer: " iTemp1


End_Procedure