新手。C#模仿一个简单聊天程序,出现System.Net.Sockets.SocketException”类型的未经处理的异常出现在 System.dll 中 聊天异常c# 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 IP或Port若有问题, 就无法连接, 当然要报错。 你有什么想不通的? tcpClient.Connect(IPAddress.Parse(textBox1.Text), Int32.Parse(textBox2.Text));NonBlockingprivate bool CheckConnectivityForProxyHost(IPEndPoint remoteEP) { bool isUp = false; try { client.SendTimeout = 5000; client.ReceiveTimeout = 5000; CallWithTimeout( 50, client, remoteEP); if (client.Connected) { isUp = true; } } catch (Exception) { isUp = false; } finally { //try //{ // if (client != null) // { // client.Shutdown(SocketShutdown.Both); // } //} //catch (Exception) //{ //} //finally //{ // if (client != null) // client.Close(); //} } return isUp; } private void CallWithTimeout( int timeoutMilliseconds, Socket socket, IPEndPoint ipendPoint) { try { Action wrappedAction = () => { ConnectToProxyServers(socket, ipendPoint); }; IAsyncResult result = wrappedAction.BeginInvoke(null, null); if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds)) { wrappedAction.EndInvoke(result); } } catch (Exception) { } } private void ConnectToProxyServers(Socket testSocket, IPEndPoint ipEndPoint) { try { if (testSocket == null || ipEndPoint == null) return; testSocket.Connect(ipEndPoint); } catch (Exception) { } } 我的想法是弹窗提示无法连接,而不是程序死掉。加个try-catch在你的代码外就行了: private void button3_Click(object sender, EventArgs e) { try { ///your code } catch(Exception ex) { MessageBox.Show(ex.Message); } } 我的想法是弹窗提示无法连接,而不是程序死掉。加个try-catch在你的代码外就行了: private void button3_Click(object sender, EventArgs e) { try { ///your code } catch(Exception ex) { MessageBox.Show(ex.Message); } } 非常感谢! 程序不会直接死掉,对于没有用到的IP Port会快速的返回结果,不会出现等待20秒以上的情况。 菜鸟提问:怎么样把上传的图片在页面上显示出来 怎样将一个自定义的object对象保存到Access中 你们说像C#这样写出程序需要装Framework才能运行,是不是缺点? 两个函数间值传递 非超级高手,无法解答,关于使用.net环境保护 win32程序,绝对创意 无后缀URL的实现方法,谁知道告诉我一下,谢谢.. 慕名而来,请高人帮帮忙,如何在unix系统下用C语言包含库函数? 上传图片出错!!帮忙看看~ 关于C#线程安全 请带我进入C#的大门 请问谁知道c#下如何进行验证码的识别?识别率高点的 ComboBox如何设置默认第一项而非空白,求助?
tcpClient.Connect(IPAddress.Parse(textBox1.Text), Int32.Parse(textBox2.Text));
NonBlockingprivate bool CheckConnectivityForProxyHost(IPEndPoint remoteEP)
{ bool isUp = false;
try
{
client.SendTimeout = 5000;
client.ReceiveTimeout = 5000;
CallWithTimeout( 50, client, remoteEP);
if (client.Connected)
{
isUp = true;
} }
catch (Exception)
{
isUp = false;
}
finally
{
//try
//{
// if (client != null)
// {
// client.Shutdown(SocketShutdown.Both);
// }
//}
//catch (Exception)
//{ //}
//finally
//{
// if (client != null)
// client.Close();
//} }
return isUp;
}
private void CallWithTimeout( int timeoutMilliseconds, Socket socket, IPEndPoint ipendPoint)
{
try
{
Action wrappedAction = () =>
{
ConnectToProxyServers(socket, ipendPoint);
}; IAsyncResult result = wrappedAction.BeginInvoke(null, null); if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds))
{
wrappedAction.EndInvoke(result);
} }
catch (Exception)
{ }
} private void ConnectToProxyServers(Socket testSocket, IPEndPoint ipEndPoint)
{
try
{
if (testSocket == null || ipEndPoint == null)
return; testSocket.Connect(ipEndPoint); }
catch (Exception)
{ }
}
加个try-catch在你的代码外就行了: private void button3_Click(object sender, EventArgs e)
{
try
{
///your code
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
加个try-catch在你的代码外就行了: private void button3_Click(object sender, EventArgs e)
{
try
{
///your code
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
非常感谢!