Asp.NET 5监听tcp socket

时间:2016-01-01 17:31:29

标签: c# asp.net sockets tcp asp.net-core

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

有没有办法在Asp.Net 5中执行此操作或有计划吗?

2 个答案:

答案 0 :(得分:1)

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

代码:

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!");
    }
}

项目文件:

{
  "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"
      }
    }
  }
}

答案 1 :(得分:0)

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