Sample code for 30+ languages & platforms
Visual FoxPro

Send a 4-byte Length Prefix over a Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.SendCount, which sends a four-byte length prefix in network byte order. This is typically used to tell the peer how many bytes follow.

Background. A length-prefixed framing scheme sends the byte count of a message before the message body, so the receiver knows exactly how many bytes to read with a counted receive.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSocket
LOCAL lnBTls
LOCAL lnMaxWaitMs
LOCAL lnMessageLength

lnSuccess = 0

loSocket = CreateObject('Chilkat.Socket')

*  Connect to the server using TLS.
lnBTls = 1
lnMaxWaitMs = 5000
lnSuccess = loSocket.Connect("example.com",5000,lnBTls,lnMaxWaitMs)
IF (lnSuccess = 0) THEN
    ? loSocket.LastErrorText
    RELEASE loSocket
    CANCEL
ENDIF

*  Send a four-byte length prefix in network byte order.  A common framing pattern is to send the
*  number of bytes in a message before the message itself, so the peer knows exactly how many bytes
*  to read.
lnMessageLength = 512
lnSuccess = loSocket.SendCount(lnMessageLength)
IF (lnSuccess = 0) THEN
    ? loSocket.LastErrorText
    RELEASE loSocket
    CANCEL
ENDIF

? "Length prefix sent."

RELEASE loSocket