Main Site Documentation

Need help reading first 4 bytes tcp response


#1

Hi,

I need to read the first 4 bytes of a server response to determine the actual length of the packet. It works if i use it in the full framework using BitConverter.ToInt32, but when i’m using the Utility.ExtractValueFromArray(length, 0, 4); i’m getting a whole nother result

Can Someone look at my code please


private static Boolean CheckPacket(Byte[] buffer)
        {
            Byte[] length = new Byte[4];
            Array.Copy(buffer, length, 4);
            UInt32 packetLength = Utility.ExtractValueFromArray(length, 0, 4);
            Debug.Print("buffer: " + buffer.Length + " packet: " + packetLength);
            return buffer.Length == packetLength;
        }


        private void ProcessDeviceRequest()
        {
            Socket socket = _tcpSocket;
            try
            {
                Byte[] buffer = null;
                while (true)
                {
                    // Wait for the client request to start to arrive.
                    const Int32 microsecondsPerSecond = 1000000;
                    if (!socket.Poll(5*microsecondsPerSecond, SelectMode.SelectRead)) continue;
                    Byte[] inBuffer = new byte[2048];
                    Int32 bytesRead = socket.Receive(inBuffer, socket.Available, SocketFlags.None);

                    if (bytesRead == 0) break;

                    if (buffer == null)
                    {
                        buffer = new byte[bytesRead];
                        Array.Copy(inBuffer, buffer, bytesRead);
                        if (CheckPacketAndSend(buffer))
                        {
                            break;
                        }
                    }
                    else
                    {
                        buffer = ConcatArray(buffer, buffer.Length, inBuffer, bytesRead);
                        if (CheckPacketAndSend(buffer))
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (!(ex is SocketException))
                {
                    Debug.Print("Socket exception client");
                }
            }
        }

The code above handles the response from the server. The CheckPacket is used in this code


        private Boolean CheckPacketAndSend(Byte[] buffer)
        {
            if (CheckPacket(buffer))
            {
                _request = RemovePrefix(buffer);
                _request = _request.Trim();
                String remote = GetRemote(_tcpSocket);
                Debug.Print("Device request from " + remote + ": [" + _request + "]");
                new Thread(SendOutputDeviceResponse).Start();
                return true;
            }
            return false;
        }

This is the result i get using the Utility.ExtractValueFromArray(length, 0, 4);

buffer: 1460 packet: 436600832
buffer: 1566 packet: 436600832

Thanx in advance.


#2

Endianess is the problem. Network is big endian


#3

I thought so :).

I Already fixed it. thanx GUS


        private static Int32 BitConvert(Byte[] buffer)
        {
            //return (buffer[0]) | ((buffer[1]) << 8) | ((buffer[2]) << 16) | ((buffer[3]) << 24);
            return ((buffer[0]) << 24) | ((buffer[1]) << 16) | ((buffer[2]) << 8) | ((buffer[3])) + 4;
        }