gpt4 book ai didi

c# - 'System.Net.Sockets.Socket.Dispose(bool)' 由于其保护级别而无法访问

转载 作者:行者123 更新时间:2023-11-30 15:36:57 25 4
gpt4 key购买 nike

我在编译时遇到了这个错误。这是什么意思,我该如何解决?

'System.Net.Sockets.Socket.Dispose(bool)' 由于其保护级别而无法访问

这是我的两个文件;

监听器.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Multi_con_Server
{
class Listener
{
Socket s;

public bool Listening
{
get;
private set;
}
public int Port
{
get;
private set;
}

public Listener(int port)
{
Port = port;
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void Start()
{
if (Listening)
return;
s.Bind(new IPEndPoint(0, Port));
s.Listen(0);

s.BeginAccept(callback, null);
Listening = true;
}
public void Stop()
{
if (!Listening)
return;

s.Close();
s.Dispose();
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}

void callback(IAsyncResult ar)
{
try
{
Socket s = this.s.EndAccept(ar);

if (SocketAccepted != null)
{
SocketAccepted(s);
}

this.s.BeginAccept(callback, null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

}
public delegate void SocketAccecptedHandler(Socket e);
public event SocketAccecptedHandler SocketAccepted;

}
}

程序.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Multi_Con_C
{
class Program
{
static void Main(string[] args)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
s.Connect("127.0.0.1", 8);
s.Close();
s.Dispose();
}
}
}

最佳答案

来自 MSDN , Socket.Close() 自动“释放所有相关资源”。您可以安全地从代码中删除所有出现的 Socket.Dispose()

关于c# - 'System.Net.Sockets.Socket.Dispose(bool)' 由于其保护级别而无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13206617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com