与另一个mac进行C#ping



我尝试过这样做,但由于我是C#编码的新手,所以收到了很多错误。

我的实际目的是ping一个静态ip,它不断地从温度传感器接收数据。我想查看家中的数据并保存数据。

    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"));
    }

    }

使用此示例,因为Microsoft的示例尚未准备好使用。。。

http://www.codeplanet.eu/tutorials/csharp/4-tcp-ip-socket-programmierung-in-csharp.html?start=4

在这个"德语"教程是完整的代码可用于发送IP。

packet.Type = ICMP_ECHO;  --> ICMP_ECHO must be set with a number (8) manual
{
    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.)
        }
}
<--mustbe create by your own 
    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"));
        }

我想你永远不会到达这个邮箱,因为你跳了"return Convert.ToInt32(ping.SendAddress.RoundstripTime);"

在返回之前设置meassagebox。。

最新更新