Sample code for 30+ languages & platforms
Xbase++

TCP Socket Send Byte and Receive Byte

Demonstrates the Chilkat Socket ReceiveByte and SendByte method.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSock
LOCAL nUseTls
LOCAL nPort
LOCAL nMaxWaitMs
LOCAL nTreatAsUnsignedInt

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 a byte.
oSock:SendByte(96)

//  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 byte back..
//  After successfully reading, the byte value is available in the ReceivedInt property.
nTreatAsUnsignedInt := 1
nSuccess := oSock:ReceiveByte(nTreatAsUnsignedInt)
IF (nSuccess == 0)
    ? oSock:LastErrorText
    oSock:destroy()
    RETURN
ENDIF

//  Let's look at the value of the byte received.  It should be 96.
? Str(oSock:ReceivedInt)

//  The echo server also echoed the LF back.
nSuccess := oSock:ReceiveByte(nTreatAsUnsignedInt)
//  Assuming success..

//  Should be decimal 10.
? Str(oSock:ReceivedInt)

oSock:destroy()