1

I'm working on a code challenge which, in short, provides a server that encodes/decodes the data sent in the request. It is a TCP/IP server with the following custom binary request/response structures:

request/response structures

I need to create a desktop client that will send requests and receive responses, and display data accordingly. I'm having trouble understanding how the request would be formed, and how I would send the request to the TCP/IP server (using C#). Could someone provide perhaps an example of what this might look like?

3
  • so you are having trouble with the whole thing? Commented Nov 16, 2017 at 1:56
  • after googling "C# byte array" and "C# send bytes TCP" where did you get stuck? Commented Nov 16, 2017 at 1:58
  • I'm stuck at understanding how the request should look. If I had an example request I might be able to figure it out. e.g. if I was trying to use "test string" as the data, what would that look like in request form? Commented Nov 16, 2017 at 2:06

1 Answer 1

2

not sure about your checksum, but constructing a packet would look something like

        byte[] CreatePacket(string s, Operation op)
        {
            var packet = new List<byte>();
            packet.AddRange(BitConverter.GetBytes((UInt16) 0x1092));
            packet.Add(1);
            var data = Encoding.ASCII.GetBytes(s);
            packet.AddRange(BitConverter.GetBytes((UInt32) (9 + data.Length)));
            packet.Add((byte) (op == Operation.Encode ? 1 : 2));
            packet.AddRange(data);
            packet.Add(CheckSum(packet));
            return packet.ToArray();
        }

given

    enum Operation
    {
        Encode,
        Decode,
    }

and

server.Send(CreatePacket("test string", Operation.Encode))

Also with the checksum spec...

        byte CheckSum(List<byte> packet)
        {
            return (byte)(packet.Select((b, i) => (value: b, index: i))
                .Sum(o => (o.value & (1 << (o.index % 8))) > 0 ? 1 :0) % 256);
        }
Sign up to request clarification or add additional context in comments.

4 Comments

FWIW, The checksum is calculated by looping through each byte and evaluating a bit of the byte in a specific position depending on the byte being evaluated. This checksum is specifically - For the 1st byte in the Request / Response, evaluate the bit in the 1st position. If the bit being evaluated is 1 then increment the checksum by 1. If the bit being evaluated is 0 do nothing. - For the 2nd byte in the Request / Response, evaluate the bit in the 2nd position. If the bit being evaluated is 1 then increment the checksum by 1. If the bit being evaluated is 0 do nothing.
also, does your solution take into consideration the fact that the length aspect expects little endian UInt32?
by default it is little endian. But it can change on other platforms ( I'm not sure there is a platfrom that is different) you can see what it is on your platform with something like Console.WriteLine(BitConverter.IsLittleEndian);
also, not sure about the checksum definition, I made some assumptions so may or may not work,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.