Unicode C
Unicode C
Receive a Single Byte from a Socket
See more Socket/SSL/TLS Examples
Demonstrates Socket.ReceiveByte, which reads exactly one byte and stores its numeric value in the ReceivedInt property. The argument selects signed or unsigned interpretation.
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 Unicode C Downloads
#include <C_CkSocketW.h>
void ChilkatSample(void)
{
BOOL success;
HCkSocketW socket;
BOOL bTls;
int maxWaitMs;
BOOL bUnsigned;
success = FALSE;
socket = CkSocketW_Create();
// Connect to the server using TLS.
bTls = TRUE;
maxWaitMs = 5000;
success = CkSocketW_Connect(socket,L"example.com",5000,bTls,maxWaitMs);
if (success == FALSE) {
wprintf(L"%s\n",CkSocketW_lastErrorText(socket));
CkSocketW_Dispose(socket);
return;
}
// Read exactly one byte. Its numeric value is stored in the ReceivedInt property. Passing TRUE
// interprets the byte as unsigned (0 through 255); FALSE interprets it as signed.
bUnsigned = TRUE;
success = CkSocketW_ReceiveByte(socket,bUnsigned);
if (success == FALSE) {
wprintf(L"%s\n",CkSocketW_lastErrorText(socket));
CkSocketW_Dispose(socket);
return;
}
wprintf(L"Received byte value: %d\n",CkSocketW_getReceivedInt(socket));
CkSocketW_Dispose(socket);
}