public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
String text = textBox1.Text;
UdpClient udpc = new UdpClient(text,8899);
IPEndPoint ep = null;
while (true)
{
MessageBox.Show("Name: ");
string name = "Connected";
if (name == "") break;
byte[] sdata = Encoding.ASCII.GetBytes(name);
udpc.Send(sdata, sdata.Length);
if (udpc.Receive(ref ep)==null)
{
MessageBox.Show("Host not found");
}
else
{
byte[] rdata = udpc.Receive(ref ep);
string job = Encoding.ASCII.GetString(rdata);
MessageBox.Show(job);
}
}
}
当我的远程机器未连接时,我收到一条错误消息。使用此代码,我得到了错误。
if (udpc.Receive(ref ep)==null)
错误消息是 未处理套接字异常(远程主机强行关闭了现有连接)