Error while trying to Ping in Unity - C#
I am trying to ping one of the servers using C# Ping. Returns an error on first execution, but claims that the operation was completed successfully.
System.Net.Sockets.SocketException (0x80004005): The operation completed successfully.\r\n\r\n at System.Net.Sockets.Socket..ctor (System.Net.Sockets.AddressFamily addressFamily, System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType) [0x00069]
Every time the code is executed after the first, it returns a different error. The code I'm using to ping looks like this, which is pretty much pulled straight from MSDN. https://msdn.microsoft.com/en-us/library/system.net.networkinformation.pingreply(v=vs.110).aspx
public static bool pingHost(string nameOrAddress)
{
bool pingable = false;
if (nameOrAddress == "127.0.0.1")
{
return true;
}
try
{
System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
PingOptions options = new PingOptions();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 3;//3 seconds?
PingReply reply = pingSender.Send(nameOrAddress, timeout);
if (reply.Status == IPStatus.Success)
{
pingable = true;
} else
{
pingable = false;
}
}
catch (PingException e)
{
Debug.Log("Error pinging: " + e.Message + " - " + e.StackTrace);
// Discard PingExceptions and return false;
}
return pingable;
}
I can ping the server from my terminal on my machine, but not through code. Why am I getting this error?
EDIT
I tested the code in a standard C# application and it works fine. This question should be something to do with unity. Unity has its own Ping class, but cannot be called from outside the main thread. The code below tries to use this, but it always returns false and -1 for a certain amount of time. Does the ping have to complete within 3 seconds? I tried increasing this time to 10 seconds, but it still returns false. The address can definitely be pinged, since I can ping it from terminal.
IEnumerator ping()
{
while (true)
{
UnityEngine.Ping pinger = new UnityEngine.Ping("ADDRESS");
yield return new WaitForSeconds(3.0f);
Debug.Log("Ping finished? " + pinger.isDone + " - " + pinger.time);
yield return new WaitForSeconds(2.0f);
}
}
Since this doesn't work, I write my paper's next question, how does Unity ping actually work? My servers are in a secure environment with certain open ports. If it does something unexpected other than the standard ping, it may not be able to do it.
EDIT 2
IEnumerator ping()
{
while (true)
{
WaitForSeconds f = new WaitForSeconds(0.05f);
Ping p = new Ping("ADDR");
while (!p.isDone)
{
yield return f;
}
PingFinished(p);
}
}
This code seems to work well, but only works from the main thread. I will need a solution that can be run from a separate task or thread.
Your answer
![](https://koobas.hobune.stream/wayback/20220613033811im_/https://answers.unity.com/themes/thub/images/avi.jpg)