简体   繁体   中英

Asp.NET 5 listen to tcp socket

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM