Xbase++
Xbase++
Socket Send and Receive BinData
Demonstrates the Chilkat Socket ReceiveBdN and SendBd methods.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oSock
LOCAL nUseTls
LOCAL nPort
LOCAL nMaxWaitMs
LOCAL oBdToSend
LOCAL i
LOCAL nByteVal
LOCAL oBdRecv
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oSock := CreateObject("Chilkat.Socket")
// --------------------------------------------------------------------
// This example uses the public TCP echo service at https://tcpbin.com/
// --------------------------------------------------------------------
nUseTls := 0
nPort := 4242
nMaxWaitMs := 5000
nSuccess := oSock:Connect("tcpbin.com", nPort, nUseTls, nMaxWaitMs)
IF (nSuccess == 0)
? oSock:LastErrorText
oSock:destroy()
RETURN
ENDIF
// Wait a max of 2 seconds for a response..
oSock:MaxReadIdleMs := 2000
// Send 26 bytes contained in a Chilkat BinData
oBdToSend := CreateObject("Chilkat.BinData")
i := 0
nByteVal := 65
DO WHILE i < 26
oBdToSend:AppendByte(nByteVal)
nByteVal := nByteVal + 1
i := i + 1
ENDDO
// Send the contents of the BinData
// Pass zero's in the 2nd and 3rd arguments to send the entire contents of the BinData.
oSock:SendBd(oBdToSend, 0, 0)
// The tcpbin.com echo server only echoes after receiving an LF (linefeed char)
oSock:SendByte(10)
// The echo server will echo back whatever is sent to it.
// We should be able to read the same bytes back..
oBdRecv := CreateObject("Chilkat.BinData")
// Receive the 26 bytes previously sent, plus the LF char.
nSuccess := oSock:ReceiveBdN(27, oBdRecv)
IF (nSuccess == 0)
? oSock:LastErrorText
oSock:destroy()
oBdToSend:destroy()
oBdRecv:destroy()
RETURN
ENDIF
// Show the contents of bdRecv in hex format
? oBdRecv:GetEncoded("hex")
// Output: 4142434445464748494A4B4C4D4E4F505152535455565758595A0A
oSock:destroy()
oBdToSend:destroy()
oBdRecv:destroy()