C# 使用C语言ping服务器
我试图使用ping类ping服务器,但在该方法返回true的10次之后,我不断得到false,这意味着服务器已关闭[?],而不是方法:C# 使用C语言ping服务器,c#,networking,network-programming,C#,Networking,Network Programming,我试图使用ping类ping服务器,但在该方法返回true的10次之后,我不断得到false,这意味着服务器已关闭[?],而不是方法: public bool IsConnectedToInternet() { Ping p = new Ping(); try { PingReply reply = p.Send("www.uic.co.il", 1000);
public bool IsConnectedToInternet()
{
Ping p = new Ping();
try
{
PingReply reply = p.Send("www.uic.co.il", 1000);
if (reply.Status == IPStatus.Success)
return true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
return false;
}
private void start_Click(object sender, EventArgs e)
{
for (; ; )
{
Console.WriteLine(IsConnectedToInternet);
}
}
为什么过了一段时间我一直在撒谎?
多谢各位 您正在向服务器发送大量请求:
for (; ; )
{
Console.WriteLine(IsConnectedToInternet);
}
将以尽可能快的速度循环发送一个又一个请求
如果您只是在编写保持活动的服务或服务状态控件,那么使用每分钟甚至每10分钟ping一次的计时器就足够了
此外,正如其他人在他们的评论中指出的那样,您在getter中执行ping操作是在滥用属性,因为调用可能需要一些时间,并且属性getter应该真正返回,如果不是立即返回,那么应该很快返回。CheckConnection方法会有更清晰的意图。我重写了您的代码
如果连接丢失,它将触发名为ConnectionLost的事件;如果再次连接,它将触发名为Connected的事件
public class NetworkStateMonitor
{
private System.Threading.Timer _timer;
bool _wasConnected = false;
public NetworkStateMonitor()
{
_timer = new System.Threading.Timer(OnPing, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10));
}
public bool CheckInternetConnection()
{
bool result = false;
Ping p = new Ping();
try
{
PingReply reply = p.Send("www.uic.co.il", 1000);
if (reply.Status == IPStatus.Success)
return true;
catch (PingException)
{
return false;
}
}
private void OnPing(object state)
{
var newState = CheckInternetConnection();
if (!newState && _wasConnected)
ConnectionLost(this, EventArgs.Empty);
else if (newState && !_wasConnected)
Connected(this, EventArgs.Empty);
_wasConnected = newState;
}
public event EventHandler ConnectionLost = delegate{};
public event EventHandler Connected = delegate{};
}
对于此页面上的其他障碍,如果将此函数重写为:
public bool CheckInternetConnection(string HostName)
{
bool result = false; // assume error
try {
Ping oPing = new Ping();
PingReply reply = oPing.Send(HostName, 3000);
if (reply.Status == IPStatus.Success){
result = true;
}
} catch (Exception E) {
// Uncomment next line to see errors
// MessageBox.Show(E.ToString());
}
return result;
}
现在使用以下命令调用:
bool IsSuccessful = CheckInternetConnection("www.uic.co.il");
你的异常处理是我很久以来见过的最糟糕的。不捕获该异常:。IsConnectedToInternet应该是一个检查连接而不是属性的方法。请考虑把这个方法移到一个方法。属性应该没有成本。在您的Catch子句中,尝试Catch Exception ex{Console.WriteLineEx.Message.toString},看看会发生什么。感谢您的回复,我会解决这个问题,@Baboon您说属性应该没有成本是什么意思?添加到每个人的评论中。bool result=false不是必需的,只需返回false。很抱歉,我没有真正理解你的意思,你说超时应该至少为1分钟?他说如果ping需要那么长的时间,你每10毫秒轮询一次服务器。因此它被淹没了。@idish-不。你太频繁地打电话给ping了。使用计时器来控制ping的发生。他说的是将这个函数移动到一个方法,不要使用无限循环来破坏服务器,看它是否启动。如果将超时时间增加到5000,并且只ping一次,您可能就没事了。@cgatian我的目标不是ping服务器一次,我想向服务器发送大量请求,当然只是为了学习。如果我想每5000毫秒调用一次方法,我必须使用无限循环。不,谢谢你努力帮助我,我真的不理解这个代码。tho,你创建了两个事件吗?这段代码对我有用吗?因为超时仍然是1000毫秒,我必须多久调用一次这些方法?超时是1000毫秒,但轮询间隔是10秒。你用for循环向服务器发送垃圾邮件。我现在明白了,我尝试过使用你的代码,但是计时器构造函数有一个错误,我不能把这些参数放在那里,它唯一的构造函数是得到一个双区间。我很抱歉一次问了太多问题,一旦成功,我会接受你的答案。