我用下面的代碼按鈕:閉幕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