[英]C# ping with another mac
我嘗試這樣做,但是由於我不熟悉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"));
}
我認為您永遠不會到達此消息框,因為您會用“返回Convert.ToInt32(ping.SendAddress.RoundtripTime);”跳轉到自動測試
將meassagebox設置為退貨之前。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.