[英]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?你能帮我解决这里出了什么问题吗?
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;
}
}
}
}
}
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.