0

これをやってみましたが、C# コーディングは初めてなので、多くのエラーが発生します。

私の実際の目的は、温度センサーから常にデータを受信して​​いる静的 IP に ping を実行することです。自宅からデータを見たい、データを保存したい。

    using System;
    using System.Net;
    using System.Net.Sockets;

    public class Pinger
    {

        public static int GetPingTime(string host)
        {

            int dwStart = 0, dwStop = 0;

            // Create a raw socket.
            Socket socket = new Socket(AddressFamily.InterNetwork,
              SocketType.Raw, ProtocolType.Icmp);

            // Get the server IPEndPoint, and convert it to an EndPoint.
            IPHostEntry serverHE = Dns.GetHostByName(host);
            IPEndPoint ipepServer = new IPEndPoint(serverHE.AddressList[0], 0);
            EndPoint epServer = (ipepServer);

            // Set the receiving endpoint to the client machine.
            IPHostEntry fromHE = Dns.GetHostByName(Dns.GetHostName());
            IPEndPoint ipEndPointFrom = new IPEndPoint(fromHE.AddressList[0], 0);
            EndPoint EndPointFrom = (ipEndPointFrom);

            // Construct the packet to send.
            int PacketSize = 0;
            IcmpPacket packet = new IcmpPacket();
            for (int j = 0; j < 1; j++)
            {
                packet.Type = ICMP_ECHO;
                packet.SubCode = 0;
                packet.CheckSum = UInt16.Parse("0");
                packet.Identifier = UInt16.Parse("45");
                packet.SequenceNumber = UInt16.Parse("0");

                int PingData = 32;
                packet.Data = new Byte[PingData];

                for (int i = 0; i < PingData; i++)
                    packet.Data[i] = (byte)'#';

                PacketSize = PingData + 8;

                Byte[] icmp_pkt_buffer = new Byte[PacketSize];
                int index = 0;
                index = Serialize(packet, icmp_pkt_buffer, PacketSize, PingData);

                // Calculate the checksum for the packet.
                double double_length = Convert.ToDouble(index);
                double dtemp = Math.Ceiling(double_length / 2);
                int cksum_buffer_length = Convert.ToInt32(dtemp);
                UInt16[] cksum_buffer = new UInt16[cksum_buffer_length];
                int icmp_header_buffer_index = 0;

                for (int i = 0; i < cksum_buffer_length; i++)
                {
                    cksum_buffer[i] = BitConverter.ToUInt16(icmp_pkt_buffer,
                      icmp_header_buffer_index);
                    icmp_header_buffer_index += 2;
                }

                UInt16 u_cksum = CheckSum(cksum_buffer, cksum_buffer_length);
                packet.CheckSum = u_cksum;

                // Now that we have the checksum, serialize the packet again.
                byte[] sendbuf = new byte[PacketSize];
                index = Serialize(packet, sendbuf, PacketSize, PingData);

                // Start timing.
                dwStart = System.Environment.TickCount;
                socket.SendTo(sendbuf, PacketSize, 0, epServer);

                // Receive the response, and then stop timing.
                byte[] ReceiveBuffer = new byte[256];
                socket.ReceiveFrom(ReceiveBuffer, 256, 0, ref EndPointFrom);
                dwStop = System.Environment.TickCount - dwStart;
            }

            // Clean up and return the calculated ping time in seconds
            socket.Close();
            return (int)dwStop;
        }

        private static int Serialize(IcmpPacket packet, byte[] buffer,
          int packetSize, int pingData)
        {

            // (Private method for serializing the packet omitted.)
        }

        private static UInt16 CheckSum(UInt16[] buffer, int size)
        {

            // (Private method for calculating the checksum omitted.)


    }
    public class PingTest
{

    private static void Main()
    {

       int GetPingMS(string hostNameOrAddress);

    System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
    return Convert.ToInt32(ping.SendAddress.RoundtripTime);

// How to call this function (IP Address).
MessageBox.Show ( GetPingMs("122.198.1.1"));

    }



    }
4

1 に答える 1