Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置套接字服务器_C#_.net_Sockets - Fatal编程技术网

C# 设置套接字服务器

C# 设置套接字服务器,c#,.net,sockets,C#,.net,Sockets,我需要一个类,它将启动套接字服务器并等待连接。这是我的密码: public static void StatServer() { TcpClient client; TcpListener tcpListener; int _serverport = 9898; tcpListener = new TcpListener(System.Net.IPAddress.Any, _serverport); try

我需要一个类,它将启动套接字服务器并等待连接。这是我的密码:

 public static void StatServer()
    {
        TcpClient client;
        TcpListener tcpListener;
        int _serverport = 9898;
        tcpListener = new TcpListener(System.Net.IPAddress.Any, _serverport);
        try
        {
            tcpListener.Start();
            while (true)
            {
                if (tcpListener.Pending())
                {
                    client = tcpListener.AcceptTcpClient();
                    Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                    clientThread.Start(client);
                }
            }
        }
        catch (SocketException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
但是,从中,我们发现代码没有
TcpClient
等,哪个代码是正确的?
我误解了这个概念吗?

我认为,两者都是正确的。我认为,TcpListener/client只是一种包装器,它在里面使用套接字

只是看了一下TcpListener的反编译代码

private Socket m_ServerSocket;

...

public TcpListener(IPEndPoint localEP)
{
  if (Logging.On)
    Logging.Enter(Logging.Sockets, (object) this, "TcpListener", (object) localEP);
  if (localEP == null)
    throw new ArgumentNullException("localEP");
  this.m_ServerSocketEP = localEP;
  this.m_ServerSocket = new Socket(this.m_ServerSocketEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  if (!Logging.On)
    return;
  Logging.Exit(Logging.Sockets, (object) this, "TcpListener", (string) null);
}
等等


我认为,TcpListener为您提供了一些舒适的方法,Socket更低级。

如果您从未使用过Socket编程,我建议您现在不要开始。而是使用一个库,例如mine,它是开源的:。只需要几行代码就可以获得一个完全工作的客户机/服务器。您正忙于等待烧掉整个CPU核心。我经常看到这个错误。我可以问一下(出于好奇)您为什么决定在接受之前检查
挂起的
?——@usr,我不知道。我没碰过这个地方。我的老板给了我一段代码。有什么改进吗?删除支票。接受块,直到有东西要接受为止。一般来说,在不了解每条线路的情况下使用插座是非常危险的。如果我可以这样说的话,一般来说,不健全的开发实践是不正确的。TcpListener和TcpClient都存在于.NETheh中。我看错了答案(前两行读错了;)