问题描述
我想有一个IP +端口异步连续ping请求。
问题是,当目标服务器脱机,整个程序中请求时冻结。但是,这不应该发生。
这是启动程序时被调用一次,我的ping功能。
它应该做一个ping请求每3秒。
公共异步无效启动(串IP)
{
秒表手表=新的秒表(); 而(真)
{
watch.Restart();
使用(TcpClient的TCP =新的TcpClient())
{
//尝试连接到一个关闭的端口
IAsyncResult的解析度= tcp.ConnectAsync(127.0.0.1,1234); 下面while循环时//整个程序冻结
//虽然这是一个异步函数
而(watch.ElapsedMilliseconds< 1000)
{
如果(tcp.Connected)
{
打破;
}
}
watch.Stop();
如果(tcp.Connected)
{
StatusControl.Text = watch.ElapsedMilliseconds.ToString()+毫秒;
}
其他
{
StatusControl.Text =离线;
}
}
//等待3secs下一个请求
等待Task.Delay(3000);
}
}
如果您想与超时一起做一个异步ping请求到一个端口,这code为我做:
专用异步任务<长>中国平安(字符串主机,诠释端口,INT超时)
{
经过长= -1;
秒表手表=新的秒表(); 使用(TcpClient的TCP =新的TcpClient())
{
尝试
{
使用(CancellationTokenSource CTS =新CancellationTokenSource())
{
StartConnection(主机,端口,TCP,手表,克拉);
等待Task.Delay(超时cts.Token);
}
}
赶上{}
最后
{
如果(tcp.Connected)
{
tcp.GetStream()关闭()。
经过= watch.ElapsedMilliseconds;
}
tcp.Close();
}
} 返回经过;
} 私人异步无效StartConnection(字符串主机,诠释端口,TCP TcpClient的,秒表手表,CancellationTokenSource克拉)
{
尝试
{
watch.Start();
等待tcp.ConnectAsync(主机,端口);
watch.Stop();
cts.Cancel();
}
赶上{}
}
这只是尽快处置该TcpClient的随着时间的结束并返回-1,当服务器脱机或出事了。
I'd like to have an asynchronous continuous ping request on a ip + port.The problem is, when the targeted server is offline, the whole program freezes during request time. But this shouldn't happen.
This is my ping function which is called once when starting the program.It should do a ping request every 3 seconds.
public async void Start(string ip)
{
Stopwatch watch = new Stopwatch();
while (true)
{
watch.Restart();
using (TcpClient tcp = new TcpClient())
{
//try to connect to a closed port
IAsyncResult res = tcp.ConnectAsync("127.0.0.1", 1234);
//during the following while-loop the whole program freezes
//although this is an asynchronous function
while (watch.ElapsedMilliseconds < 1000)
{
if (tcp.Connected)
{
break;
}
}
watch.Stop();
if (tcp.Connected)
{
StatusControl.Text = watch.ElapsedMilliseconds.ToString() + " ms";
}
else
{
StatusControl.Text = "Offline";
}
}
//wait 3secs for the next request
await Task.Delay(3000);
}
}
If you want to do an asynchronous ping request to a port along with a timeout, this code did it for me:
private async Task<long> Ping(string host, int port, int timeOut)
{
long elapsed = -1;
Stopwatch watch = new Stopwatch();
using (TcpClient tcp = new TcpClient())
{
try
{
using (CancellationTokenSource cts = new CancellationTokenSource())
{
StartConnection(host, port, tcp, watch, cts);
await Task.Delay(timeOut, cts.Token);
}
}
catch {}
finally
{
if (tcp.Connected)
{
tcp.GetStream().Close();
elapsed = watch.ElapsedMilliseconds;
}
tcp.Close();
}
}
return elapsed;
}
private async void StartConnection(string host, int port, TcpClient tcp, Stopwatch watch, CancellationTokenSource cts)
{
try
{
watch.Start();
await tcp.ConnectAsync(host,port);
watch.Stop();
cts.Cancel();
}
catch {}
}
It simply disposes the tcpClient as soon as the time is over and returns -1 when the server is offline or something went wrong.
这篇关于TCP连接的客户端程序冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!