public static int Main(string [] args) { String strHostName = new String(""); if (args.Length == 0) { // Getting Ip address of local machine... // First get the host name of local machine. strHostName = DNS.GetHostName(); Console.WriteLine("Local Machine's Host Name: " + strHostName); } else { strHostName = args[0]; } // Then using host name, get the IP address list.. IPHostEntry ipEntry = DNS.GetHostByName(strHostName); IPAddress [] addr = ipEntry.AddressList; for (int i = 0; i < addr.Length; i++) { Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString()); } return(0); }
public int GetPingTime(string host) { int PingTime = 0; IPHostEntry serverHE, fromHE; int nBytes = 0; int dwStart = 0, dwStop = 0; IcmpPacket packet = new IcmpPacket(); if (host == null) { return(-1); } Socket socket = new Socket(AddressFamily.AfINet, SocketType.SockRaw, ProtocolType.ProtICMP); // Get the server endpoint serverHE = DNS.GetHostByName(host); if (serverHE == null) { return(-1); // fail } // Convert the server IP_EndPoint to an EndPoint IPEndPoint ipepServer = new IPEndPoint(serverHE.AddressList[0], 0); EndPoint epServer = (ipepServer); // Set the receiving endpoint to the client machine fromHE = DNS.GetHostByName(DNS.GetHostName()); IPEndPoint ipEndPointFrom = new IPEndPoint(fromHE.AddressList[0], 0); EndPoint EndPointFrom = (ipEndPointFrom); int PacketSize = 0; for (int j = 0; j < 1; j++) { // Construct the packet to send 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; // sizeof(IcmpPacket) - 8; packet.Data = new Byte[PingData]; for (int i = 0; i < PingData; i++) { packet.Data[i] = (byte)'#'; } PacketSize = PingData + 8; // // since we have to serialize the final byte array into a UInt16 array, // let us make our life easy by making sure that the icmp_pkt_buffer // byte array is of an even size ( > PacketSize ) if the size of the IcmpPacket // is not even // if (PacketSize % 2 == 1) { ++PacketSize; } Byte [] icmp_pkt_buffer = new Byte[PacketSize]; Int32 Index = 0; Index = Serialize( packet, icmp_pkt_buffer, PacketSize, PingData); if (Index == -1) { return(-1); } // // now get this critter into a UInt16 array // Double double_length = Convert.ToDouble(Index); Double dtemp = Math.Ceil(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); if (Index == -1) { return(-1); } dwStart = System.Environment.TickCount; // Start timing if ((nBytes = socket.SendTo(sendbuf, PacketSize, 0, epServer)) == SOCKET_ERROR) { Console.WriteLine("Error calling sendto"); return(-1); // fail } // Initialize the buffers. The receive buffer is the size of the // ICMP header plus the IP header (20 bytes) Byte [] ReceiveBuffer = new Byte[256]; nBytes = 0; nBytes = socket.ReceiveFrom(ReceiveBuffer, 256, 0, ref EndPointFrom); if (nBytes == SOCKET_ERROR) { dwStop = SOCKET_ERROR; } else { dwStop = System.Environment.TickCount - dwStart; // stop timing } } socket.Close(); PingTime = (int)dwStop; return(PingTime); }
private void GetGlobalHostName() { GlobalHostName = DNS.GetHostName(); GlobalHostName = DNS.GetHostByName(GlobalHostName).Hostname; GlobalIPAddress = DNS.GetHostByName(GlobalHostName).AddressList[0].ToString(); }
/// <summary> /// This method takes the "hostname" of the server /// and then it ping's it and shows the response time /// </summary> public static void PingHost(string host) { //Declare the IPHostEntry IPHostEntry serverHE, fromHE; int nBytes = 0; int dwStart = 0, dwStop = 0; //Initilize a Socket of the Type ICMP Socket socket = new Socket(AddressFamily.AfINet, SocketType.SockRaw, ProtocolType.ProtICMP); // Get the server endpoint try { serverHE = DNS.GetHostByName(host); } catch (Exception) { Console.WriteLine("Host not found"); // fail return; } // Convert the server IP_EndPoint to an EndPoint IPEndPoint ipepServer = new IPEndPoint(serverHE.AddressList[0], 0); EndPoint epServer = (ipepServer); // Set the receiving endpoint to the client machine fromHE = DNS.GetHostByName(DNS.GetHostName()); IPEndPoint ipEndPointFrom = new IPEndPoint(fromHE.AddressList[0], 0); EndPoint EndPointFrom = (ipEndPointFrom); int PacketSize = 0; IcmpPacket packet = new IcmpPacket(); // Construct the packet to send packet.Type = ICMP_ECHO; //8 packet.SubCode = 0; packet.CheckSum = UInt16.Parse("0"); packet.Identifier = UInt16.Parse("45"); packet.SequenceNumber = UInt16.Parse("0"); int PingData = 32; // sizeof(IcmpPacket) - 8; packet.Data = new Byte[PingData]; //Initilize the Packet.Data for (int i = 0; i < PingData; i++) { packet.Data[i] = (byte)'#'; } //Variable to hold the total Packet size PacketSize = PingData + 8; Byte [] icmp_pkt_buffer = new Byte[PacketSize]; Int32 Index = 0; //Call a Method Serialize which counts //The total number of Bytes in the Packet Index = Serialize( packet, icmp_pkt_buffer, PacketSize, PingData); //Error in Packet Size if (Index == -1) { Console.WriteLine("Error in Making Packet"); return; } // now get this critter into a UInt16 array //Get the Half size of the Packet Double double_length = Convert.ToDouble(Index); Double dtemp = Math.Ceil(double_length / 2); int cksum_buffer_length = Convert.ToInt32(dtemp); //Create a Byte Array UInt16 [] cksum_buffer = new UInt16[cksum_buffer_length]; //Code to initialize the Uint16 array 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; } //Call a method which will return a checksum UInt16 u_cksum = checksum(cksum_buffer, cksum_buffer_length); //Save the checksum to the Packet packet.CheckSum = u_cksum; // Now that we have the checksum, serialize the packet again Byte [] sendbuf = new Byte[PacketSize]; //again check the packet size Index = Serialize( packet, sendbuf, PacketSize, PingData); //if there is a error report it if (Index == -1) { Console.WriteLine("Error in Making Packet"); return; } dwStart = System.Environment.TickCount; // Start timing //send the Pack over the socket if ((nBytes = socket.SendTo(sendbuf, PacketSize, 0, epServer)) == SOCKET_ERROR) { Console.WriteLine("Socket Error cannot Send Packet"); } // Initialize the buffers. The receive buffer is the size of the // ICMP header plus the IP header (20 bytes) Byte [] ReceiveBuffer = new Byte[256]; nBytes = 0; //Receive the bytes bool recd = false; int timeout = 0; //loop for checking the time of the server responding while (!recd) { nBytes = socket.ReceiveFrom(ReceiveBuffer, 256, 0, ref EndPointFrom); if (nBytes == SOCKET_ERROR) { Console.WriteLine("Host not Responding"); recd = true; break; } else if (nBytes > 0) { dwStop = System.Environment.TickCount - dwStart; // stop timing Console.WriteLine("Reply from " + epServer.ToString() + " in " + dwStop + "MS :Bytes Received" + nBytes); recd = true; break; } timeout = System.Environment.TickCount - dwStart; if (timeout > 1000) { Console.WriteLine("Time Out"); recd = true; } } //close the socket socket.Close(); }