我用下面的代码按钮:闭幕C#插座
private void button1_Click(object sender, EventArgs e)
{
IPHostEntry host = Dns.GetHostEntry(entered_ip);
foreach (var address in host.AddressList)
{
var ipe = new IPEndPoint(address, 7779);
var samp = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
samp.Connect(ipe);
if (samp.Connected)
{
enable_anticheat();
Process.Start("samp://" + entered_ip + ":" + entered_port);
break;
}
else
{
continue;
}
}
}
我希望在应用程序关闭,关闭插座samp
。但如何关闭? 据我所知,套接字通过调用samp.Close()
关闭,但如果我添加此在FormClosing
事件的形式,我得到的错误element does not exist in the current context
。
我想使用的代码是:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
samp.Close();
}
感谢。
你的变量套接字应该作为你的类的成员在函数之外声明。 – MiltoxBeyond
我知道,但如何?请帮助:( – Dewagg