【发布时间】:2017-01-23 00:07:57
【问题描述】:
我的期望是,如果端点不可用,UdpClient.Connect() 方法会抛出异常,我可以捕获并更改标签的文本以说明程序是否连接到服务器。但是,尽管我关闭了我尝试连接的服务器,但该方法完成没有问题。有什么方法可以解决这个问题,或者我应该尝试其他方法吗?
我当前的代码(IP 地址已空白,但有效):
UdpClient chatConnection = new UdpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("xxx.xx.xxx.xxx"), 1000);
// Initialize client/server connection and set status text
try
{
chatConnection.Connect(serverEndPoint);
SL_Status.Text = "Connected";
}
catch (Exception ex)
{
SL_Status.Text = "Not Connected";
MessageBox.Show("Unable to connect to server. See console for logs.");
Console.WriteLine(ex);
}
【问题讨论】: