fbpx

Tough TCP interview question on C++ with sockets and network

(Last Updated On: May 12, 2010)

Code should be in C or C++ and should be ‘production quality’. I.e. has to check for errors at runtime, and cannot overflow or underflow buffers or corrupt memory.
• Pls include a paragraph explaining how you would test this:

TCP:
Implement a routine that processes packets from the network over a reliable TCP connection:

HRESULT ProcessData(PBYTE pDataBuffer, IN ULONG cbBufferSize)// you need to implement this routine

Information:

typedef struct tag_HEADER
{
ULONG cbTotalPacketLen;
ULONG cbThisChunkLen;
#define FLAG_FIRST_CHUNK_IN_PACKET 1
#define FLAG_LAST_CHUNK_IN_PACKET 2
ULONG flags;
} CHUNK_HEADER;

The format the server sends will be a sequence of PACKETs.
Each packet is broken up into a sequence of one or more CHUNKs. Each Chunk starts with a CHUNK_HEADER.
The first CHUNK in a packet has ‘flags’ field set to FLAG_FIRST_CHUNK_IN_PACKET.
The last chunk that has ‘flags’ FLAG_LAST_CHUNK_IN_PACKET sets.
Chunks in the ‘middle’ of packet will have neither flag set.
A chunk can be any size up to 16384 bytes. A packet can be any size unbounded.

The ‘total’ length of the packet will be cbTotalPacketLen, each chunk in the packet will have this same length set in the chunk header.
The ‘length’ of the current chunk will be always ‘cbThisChunkLen’.

The ProcessData(…) routine will be called many times, with whatever data has arrived from the network. The network is reliable so data will arrive in sequence it was sent. However as this is a TCP stream, not a message there is no guarantee that the call to ProcessData will correspond to a complete chunk. I

That is important point, ProcessData may be called many times for even one CHUNK. And may be called with an arbitrary number of bytes each time ranging from just one byte.

Your goal: with ProcessData(…) is to implement a routine that reads the stream and the CHUNKs and from that reconstructs a _complete_ Packet (i.e. has data from all chunks in the packet). Once a packet is constructed you should pass it to a routine:

HRESULT ProcessPacket(PBYTE pPacketBytes, IN ULONG cbCompletePacketSize); //you do not need to implement this. Just call it EVERY time you have a completed packet.
7

Hi All,

the solution can be::::::

HRESULT ProcessData(PBYTE pDataBuffer, IN ULONG cbBufferSize)//

typedef struct tag_HEADER
{
ULONG cbTotalPacketLen;
ULONG cbThisChunkLen;
#define FLAG_FIRST_CHUNK_IN_PACKET 1
#define FLAG_LAST_CHUNK_IN_PACKET 2
ULONG flags;
} CHUNK_HEADER;

HRESULT ProcessData(PBYTE pDataBuffer, IN ULONG cbBufferSize) {

CHUNK_HEADER *header = (CHUNK_HEADER *) malloc(sizeof(CHUNK_HEADER));
static PBYTE *packet;
static int length;
header = (CHUNK_HEADER *)pDataBuffer;

if (header->flags == FLAG_FIRST_CHUNK_IN_PACKET) {
packet = NULL;
packet = (char *) malloc(header->cbTotalPacketLen);
length = header->cbThisChunkLen;
}

elseif(header->flags == FLAG_LAST_CHUNK_IN_PACKET) {
memcpy(packet+length, pDataBuffer+sizeof(CHUNK_HEADER), header->cbThisChunkLen);
ProcessPacket(packet);
packet = NULL;
length = 0;

}
else {

memcpy(packet+length, pDataBuffer+sizeof(CHUNK_HEADER), header->cbThisChunkLen);
}

IF this is called with only 1 byte from the very beginning, you’ll get wrong flag.

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Subscribe For Latest Updates

Sign up to best of business news, informed analysis and opinions on what matters to you.
Invalid email address
We promise not to spam you. You can unsubscribe at any time.

NOTE!

Check NEW site on stock forex and ETF analysis and automation

Scroll to Top