简体   繁体   English

C# 错误 CS0246 找不到类型或命名空间名称“Socket”(您是否缺少 using 指令或程序集引用)

[英]C# error CS0246 The type or namespace name 'Socket' could not be found (are you missing a using directive or an assembly reference)

I write a client-server program in C# to send words or libraries to client and to get their answer such as if they want to add new words or libraries to a dictionary or not.我在 C# 中编写了一个客户端 - 服务器程序,将单词或库发送到客户端并获得他们的答案,例如他们是否想将新单词或库添加到字典中。 My problem is the libraries System.Net.Sockets and System.Net are not recognized or properly connected.我的问题是库 System.Net.Sockets 和 System.Net 无法识别或正确连接。 Would you Plz help me with what is going wrong here?你能帮我解决这里出了什么问题吗?

here is my code:这是我的代码:

using System;    
using System.Net;    
using System.Net.Sockets;    
using System.Text;    
using System.Threading;    

namespace Dictionary.Net.SocketWrappers    
{    
    public class ServerSocket
    {
        private Func<string, string> _getMessage;
        private Socket _socket;
        public readonly int backlog;
        public readonly Encoding encoding;
        public ServerSocket(int port, int backlog, Func<string, string> getMessage, Encoding encoding)
        {
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var iPEndPoint = new IPEndPoint(IPAddress.Any, port);
            _socket.Bind(iPEndPoint);
            this.backlog = backlog;
            this._getMessage = getMessage;
            this.encoding = encoding;
        }

        public void Run()
        {
            try
            {
                _socket.Listen(backlog);
            }
            catch (SocketException)
            {
                return;
            }
            while (true)
            {
                try
                {
                    var client = _socket.Accept();
                    if (client != null)
                        ThreadPool.QueueUserWorkItem(ClientThread, client);
                }
                catch (SocketException)
                {
                    continue;
                }
            }
        }

        private void ClientThread(object newClientObject)
        {
            var newClient = newClientObject as Socket;
            var clientSocket = new ClientSocket(newClient, encoding);
            bool IsListening = true;
            while (IsListening)
            {
                try
                {
                    var receivedData = clientSocket.Receive();
                    var dataToSend = _getMessage(receivedData);
                    clientSocket.Send(dataToSend);
                }
                catch (SocketException)
                {
                    IsListening = false;
                }
            }
        }
    }
}

Screenshot of all references which I have我拥有的所有参考的屏幕截图

As Patrick Hofman already answered, you're missing a reference to System.Net.Sockets .正如 Patrick Hofman 已经回答的那样,您缺少对System.Net.Sockets的引用。

You can use the Visual Studio Package Manager to get it from NuGet.可以使用 Visual Studio 包管理器从 NuGet 获取它。

The window can be accessed over the menu with VIEW > Other Windows > Package Manager Console可以使用VIEW > Other Windows > Package Manager Console通过菜单访问该窗口

数据包管理器的位置

In the console, you have to simple write your requested package.在控制台中,您必须简单地编写您请求的包。

So in your case write Install-Package System.Net.Sockets .所以在你的情况下写Install-Package System.Net.Sockets

控制台窗口

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 错误:错误 CS0246 找不到类型或命名空间名称“”(您是否缺少 using 指令或程序集引用?) - C# Error: Error CS0246 The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246:找不到类型或命名空间名称“Npgsql”(您是否缺少 using 指令或程序集引用?) - error CS0246: The type or namespace name 'Npgsql' could not be found (are you missing a using directive or an assembly reference?) 错误CS0246找不到类型或名称空间名称“ Windows”(是否缺少using指令或程序集引用?) - Error CS0246 The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246:找不到类型或命名空间名称“StreamingContext”(是否缺少 using 指令或程序集引用?) - Error CS0246: The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246:找不到类型或命名空间名称“IWebHostEnvironment”(您是否缺少 using 指令或程序集引用?) - error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246:找不到类型或命名空间名称“BannerPosition”是否缺少 using 指令或程序集引用? - error CS0246: The type or namespace name 'BannerPosition' could not be found are you missing a using directive or an assembly reference? 错误 CS0246 找不到类型或命名空间名称“CreateRandomAnswersForKey”(您是否缺少 using 指令或程序集引用?)? - Error CS0246 The type or namespace name 'CreateRandomAnswersForKey' could not be found (are you missing a using directive or an assembly reference?)? CS0246:找不到类型或名称空间名称“ T”。 您是否缺少using指令或程序集引用? 在C#中 - CS0246: The type or namespace name `T' could not be found. Are you missing a using directive or an assembly reference? in c# CS0246 C#找不到类型或名称空间名称“ ForeignKeyAttribute”(是否缺少using指令或程序集引用?) - CS0246 C# The type or namespace name 'ForeignKeyAttribute' could not be found (are you missing a using directive or an assembly reference?) CS0246:找不到类型或命名空间名称&#39;MySql&#39;(您是否缺少using指令或程序集引用 - CS0246: The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM