AnimatGuiCtrls.Network.IcmpPacket.ToByteArray C# (CSharp) Method

ToByteArray() public method

Converts an IcmpPacket to a byte array.
public ToByteArray ( ) : byte[]
return byte[]
        public byte[] ToByteArray()
        {
            //Variable to hold the total Packet size
            byte[] buffer = new byte[ICMP_PACKET_SIZE];
            int index = 0;

            byte[] b_type = new byte[1];
            b_type[0] = (this.Type);

            byte[] b_code = new byte[1];
            b_code[0] = (this.SubCode);

            byte[] b_cksum = BitConverter.GetBytes(this.CheckSum);
            byte[] b_id = BitConverter.GetBytes(this.Identifier);
            byte[] b_seq = BitConverter.GetBytes(this.SequenceNumber);

            Array.Copy(b_type, 0, buffer, index, b_type.Length);
            index += b_type.Length;

            Array.Copy(b_code, 0, buffer, index, b_code.Length);
            index += b_code.Length;

            Array.Copy(b_cksum, 0, buffer, index, b_cksum.Length);
            index += b_cksum.Length;

            Array.Copy(b_id, 0, buffer, index, b_id.Length);
            index += b_id.Length;

            Array.Copy(b_seq, 0, buffer, index, b_seq.Length);
            index += b_seq.Length;

            // copy the data
            Array.Copy(this.Data, 0, buffer, index, PING_DATA_SIZE);
            index += PING_DATA_SIZE;

            if (index != ICMP_PACKET_SIZE) //sizeof(IcmpPacket)
                return null;
            else
                return buffer;
        }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Attempts to ping a host.
        /// </summary>
        /// <param name="serverEndPoint">EndPoint to ping</param>
        /// <param name="pingCount">Ping count</param>
        /// <returns>Ping results</returns>
        public PingResponse PingHost(IPEndPoint serverEndPoint, int pingCount)
        {
            cancel = false;

            PingResponse response = new PingResponse();

            try
            {
                //Initialize a Socket of the Type ICMP
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, this.pingTimeout);

                // Set the receiving endpoint to the client machine
                IPHostEntry clientHostEntry = Dns.GetHostEntry(Dns.GetHostName());
                EndPoint    clientEndPoint  = (new IPEndPoint(clientHostEntry.AddressList[0], 0));

                //Create icmp packet
                IcmpPacket packet = new IcmpPacket();

                // Convert icmp packet to byte array to send over socket
                byte[] sendBuffer = packet.ToByteArray();
                if (sendBuffer == null)
                {
                    OnPingError(PingResponseType.InternalError, "Could not copy ICMP packet to byte array");

                    response.PingResult   = PingResponseType.InternalError;
                    response.ErrorMessage = "Could not copy ICMP packet to byte array";
                }
                else
                {
                    response = SendPackets(socket, clientEndPoint, serverEndPoint, sendBuffer, pingCount);
                }
            }
            catch (Exception ex)
            {
                OnPingError(PingResponseType.InternalError, ex.Message);

                response.PingResult   = PingResponseType.InternalError;
                response.ErrorMessage = ex.Message;
            }

            return(response);
        }
All Usage Examples Of AnimatGuiCtrls.Network.IcmpPacket::ToByteArray