Asp.NET 5监听tcp socket

时间:2022-08-29 03:35:33

System.Net.Sockets seems to only be available in Asp.Net 4.5/4.6.

System.Net.Sockets似乎只能在Asp.Net 4.5 / 4.6中使用。

Is there a way to do this in Asp.Net 5 or are there plans to?

有没有办法在Asp.Net 5中执行此操作,还是有计划?

2 个解决方案

#1


1  

You just need a dependency on the System.Net.Sockets nuget package. Here's a complete example:

您只需要依赖System.Net.Sockets nuget包。这是一个完整的例子:

Code:

码:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;

public class Program
{
    public static void Main(string[] args)
    {
        Listen().Wait();
    }

    static async Task Listen()
    {
        var listener = new TcpListener(IPAddress.Any, 10000);
        listener.Start();
        Console.WriteLine("Waiting for a connection");
        var socket = await listener.AcceptSocketAsync();
        Console.WriteLine("Client connected!");
    }
}

Project file:

项目文件:

{
  "version": "1.0.0-*",
  "description": "TestVSCode Console Application",
  "dependencies": {
    "System.Net.Sockets": "4.1.0-beta-23516",
  },

  "frameworks": {
    "dnxcore50": {
      "dependencies": {
        "System.Console": "4.0.0-beta-23516"
      }
    }
  }
}

#2


0  

According to this answer: https://*.com/a/32302367/1977808, you must also include "System.Private.Networking" in your package.json file in order to use "System.Net.Sockets", although I am unsure of why this is.

根据这个答案:https://*.com/a/32302367/1977808,您还必须在package.json文件中包含“System.Private.Networking”才能使用“System.Net.Sockets”,尽管我我不确定为什么会这样。

#1


1  

You just need a dependency on the System.Net.Sockets nuget package. Here's a complete example:

您只需要依赖System.Net.Sockets nuget包。这是一个完整的例子:

Code:

码:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;

public class Program
{
    public static void Main(string[] args)
    {
        Listen().Wait();
    }

    static async Task Listen()
    {
        var listener = new TcpListener(IPAddress.Any, 10000);
        listener.Start();
        Console.WriteLine("Waiting for a connection");
        var socket = await listener.AcceptSocketAsync();
        Console.WriteLine("Client connected!");
    }
}

Project file:

项目文件:

{
  "version": "1.0.0-*",
  "description": "TestVSCode Console Application",
  "dependencies": {
    "System.Net.Sockets": "4.1.0-beta-23516",
  },

  "frameworks": {
    "dnxcore50": {
      "dependencies": {
        "System.Console": "4.0.0-beta-23516"
      }
    }
  }
}

#2


0  

According to this answer: https://*.com/a/32302367/1977808, you must also include "System.Private.Networking" in your package.json file in order to use "System.Net.Sockets", although I am unsure of why this is.

根据这个答案:https://*.com/a/32302367/1977808,您还必须在package.json文件中包含“System.Private.Networking”才能使用“System.Net.Sockets”,尽管我我不确定为什么会这样。