ToByteArray() public method

Converts an IcmpPacket to a byte array.
public ToByteArray ( ) : byte[]
return byte[]
Ejemplo n.º 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);
        }
Ejemplo n.º 2
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;
        }