c# socketexception
场景:新手。C#模仿一个简单聊天程序,出现System.Net.Sockets.SocketException”类型的未经处理的错误出现在 System.dll 中
新手。C#模仿一个简单聊天程序,出现System.Net.Sockets.SocketException”类型的未经处理的异常出现在 System.dll 中
在客户端连接服务器时,想判断是否连上。服务器打开,IP和Port都正确时候,没有问题。改动任何一个,就会出现错误,且只能在任务管理器结束才能返回调试。

代码:
private void button3_Click(object sender, EventArgs e)
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(IPAddress.Parse(textBox1.Text), Int32.Parse(textBox2.Text));
if (tcpClient.Connected == false)
{
MessageBox.Show("False", "Connected?");
tcpClient.Close();
}
else
{
MessageBox.Show("Okay", "Connected?");
}
}
------解决方案--------------------
加个try-catch在你的代码外就行了:
新手。C#模仿一个简单聊天程序,出现System.Net.Sockets.SocketException”类型的未经处理的异常出现在 System.dll 中
在客户端连接服务器时,想判断是否连上。服务器打开,IP和Port都正确时候,没有问题。改动任何一个,就会出现错误,且只能在任务管理器结束才能返回调试。
代码:
private void button3_Click(object sender, EventArgs e)
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(IPAddress.Parse(textBox1.Text), Int32.Parse(textBox2.Text));
if (tcpClient.Connected == false)
{
MessageBox.Show("False", "Connected?");
tcpClient.Close();
}
else
{
MessageBox.Show("Okay", "Connected?");
}
}
------解决方案--------------------
加个try-catch在你的代码外就行了:
private void button3_Click(object sender, EventArgs e)
{
try
{
///your code
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}