Click here to Skip to main content
12,623,511 members (27,840 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
this class work excellent with 32 bit but dosn't with 64 bit

any body can help me to fix it ?


using System;
using System.Net;
using System.Runtime.InteropServices;
 
namespace testPing
{
    class Class1
    {
        public static class ServerStatus
        {
 
            [StructLayout(LayoutKind.Sequential, Pack = 1)]
            public struct ICMP_ECHO_REPLY
            {
                public uint Address;
                public uint Status;
                public uint RoundTripTime;
                public ushort DataSize;
                public ushort Reserved;
                public IntPtr Data;
                public IP_OPTION_INFORMATION Options;
            }
 
            [StructLayout(LayoutKind.Sequential, Pack = 1)]
            public struct IP_OPTION_INFORMATION
            {
                public byte TTL;
                public byte TOS;
                public byte Flags;
                public byte OptionsSize;
                public IntPtr OptionsData;
                public int RealOptionData;
            }
 
            public static class Icmp
            {
                public const int IP_SUCCESS = 0;
                public const int IP_BUF_TOO_SMALL = 11001;
                public const int IP_REQ_TIMED_OUT = 11010;
                [DllImport("icmp.dll")]
                public static extern IntPtr IcmpCreateFile();
                [DllImport("icmp.dll")]
                public static extern uint IcmpSendEcho(IntPtr icmpHandle, uint
                ipAddr, ref int requestData, ushort requestSize, IntPtr optionInfo, ref
ICMP_ECHO_REPLY replyBuffer, uint replySize, int timeout);
                [DllImport("icmp.dll")]
                public static extern bool IcmpCloseHandle(IntPtr icmpHandle);
                public static bool Ping(string host)
                {
                    uint addr =
                    BitConverter.ToUInt32(IPAddress.Parse(host).GetAddressBytes(), 0);
                    IntPtr h = IcmpCreateFile();
                    int req = 123456789;
                    ICMP_ECHO_REPLY rep = new ICMP_ECHO_REPLY();
                    uint retval = IcmpSendEcho(h, addr, ref req, 4, IntPtr.Zero,
                    ref rep, 56, 10);
                    IcmpCloseHandle(h);
                    return (retval != 0 && rep.Status == IP_SUCCESS);
                }
            }
        }
 
    }
}
Posted 1-Jan-13 6:15am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

And what's wrong with this one: http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx[^]?

PS: doesn't work is not an error message, nor a concrete result.
PS2: have you checked if the 64 bit version of those api calls is the same as in 32 bit systems? Read this one: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366050(v=vs.85).aspx[^], and look for "On a 64-bit platform..."
  Permalink  
Comments
EgyptianRobot 2-Jan-13 3:33am
   
Ping Class work properly in all cases except in one only case its

" when the server is connected and after awhile the server became disconnected "

in this case the ping class get the same reply as if the server is connected....
Zoltán Zörgő 2-Jan-13 3:53am
   
I don't understand.
EgyptianRobot 2-Jan-13 5:45am
   
imagine there's client and server now and the server is connected to the network and everything is okay.
now ---- > reply.Status == IPStatus.Success
if i disconnect the server now the result still the same IPStatus.Success
------------
when I log off the client OS and then log on
the status change
Zoltán Zörgő 2-Jan-13 6:55am
   
Not only the status matters. You have to check the other properties in Options field. Try this sample:

using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
// args[0] can be an IPaddress or host name.
public static void Main (string[] args)
{
Ping pingSender = new Ping ();

// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);

// Wait 10 seconds for a reply.
int timeout = 10000;

// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions (64, true);

// Send the request.
PingReply reply = pingSender.Send (args[0], timeout, buffer, options);

if (reply.Status == IPStatus.Success)
{
Console.WriteLine ("Address: {0}", reply.Address.ToString ());
Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
}
else
{
Console.WriteLine (reply.Status);
}
}
}
}
EgyptianRobot 2-Jan-13 16:56pm
   
thanks million for your efforts but I don't know why this code dosn't work with 64 bit.
may be i have something wrong..i still working on it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161128.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100