locked
How to tcp/ip header RRS feed

  • Question

  • User-1379762086 posted

    Dear all, the below code is how am returning a tcp message to a client machine, but the company demanded i should be adding a tcp/ip header to the message. They said the header should be a  2-byte header prefixed which will indicate the length on the TCP/IP stream. can anybody help me on how i can include the header on the below code

    	private static void Send(Socket handler, String data)
    	{
    		// Convert the string data to byte data using ASCII encoding. 
    		byte[] byteData = Encoding.ASCII.GetBytes(data);
    		// Begin sending the data to the remote device. 
    		handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
    	}

    Wednesday, March 21, 2018 11:30 AM

All replies

  • User475983607 posted

    Ask the users if they want big or little endian.  IMHO, this question is out-of-scope for an ASP forum though.  

        class Program
        {
            static void Main(string[] args)
            {
                string temp = new string('A', 0xABCD);
                byte[] message = BuildMessage(temp);
            }
    
            private static byte[] BuildMessage(String data)
            {
                // Convert the string data to byte data using ASCII encoding. 
                byte[] byteData = Encoding.ASCII.GetBytes(data);
    
                if ((byteData.Count() & 0xFF0000) > 1)
                {
                    throw new ArgumentOutOfRangeException("Message cannot be larger than 0xFFFF bytes.");
                }
    
                int length = byteData.Count();
                Console.WriteLine("{0:X} ({0})", length);
                Console.WriteLine();
    
                List<byte> littleEndian = new List<byte>() { (byte)(length & 0xFF), (byte)((length >> 8) & 0xFF) };
                littleEndian.AddRange(byteData);
    
                List<byte> bigEndian = new List<byte>() { (byte)((length >> 8) & 0xFF), (byte)(length & 0xFF) };
                bigEndian.AddRange(byteData);
    
    
                Console.WriteLine("Little Endian");
                Console.WriteLine("Length:\t{0}", littleEndian.Count());
                Console.WriteLine("Byte[0]:\t{0:X}", littleEndian[0]);
                Console.WriteLine("Byte[1]:\t{0:X}", littleEndian[1]);
                Console.WriteLine("Byte[2]:\t{0:X}", littleEndian[2]);
    
                Console.WriteLine();
                Console.WriteLine("Big Endian");
                Console.WriteLine("Length:\t{0}", bigEndian.Count());
                Console.WriteLine("Byte[0]:\t{0:X}", bigEndian[0]);
                Console.WriteLine("Byte[1]:\t{0:X}", bigEndian[1]);
                Console.WriteLine("Byte[2]:\t{0:X}", bigEndian[2]);
    
    
                return littleEndian.ToArray();
    
            }
        }

    ABCD (43981)
    
    Little Endian
    Length: 43983
    Byte[0]:        CD
    Byte[1]:        AB
    Byte[2]:        41
    
    Big Endian
    Length: 43983
    Byte[0]:        AB
    Byte[1]:        CD
    Byte[2]:        41
    

    Wednesday, March 21, 2018 5:44 PM