Sample code for 30+ languages & platforms
Lianja

Receive a 4-byte Length Prefix from a Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveCount, which reads a four-byte length prefix as a signed 32-bit integer. A return value of -1 indicates failure.

Background. Reading the length prefix first tells the application exactly how many bytes to read for the message body, which is a reliable way to know when a full message has arrived.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

loSocket = createobject("CkSocket")

//  Connect to the server using TLS.
llBTls = .T.
lnMaxWaitMs = 5000
llSuccess = loSocket.Connect("example.com",5000,llBTls,lnMaxWaitMs)
if (llSuccess = .F.) then
    ? loSocket.LastErrorText
    release loSocket
    return
endif

//  Read a four-byte length prefix as a signed 32-bit integer (network byte order by default).  A
//  return value of -1 indicates failure.
lnMessageLength = loSocket.ReceiveCount()
if (lnMessageLength < 0) then
    ? loSocket.LastErrorText
    release loSocket
    return
endif

? "The peer will send " + str(lnMessageLength) + " bytes."


release loSocket