Socket.SendAsync错误的参数

时间:2017-10-08 15:49:46

标签: c# sockets

我目前正在研究ASP.NET MVC并遇到了一个问题。在我正在学习的讲座中,他们使用Socket类中的方法 - SendAsync,但是当我尝试使用它时,它只要求一个SocketAsyncEventArgs类型的参数,而在讲座中它要求两个论点 - 一个ArraySegment<byte>和一个SocketFlag Enum。这怎么可能?我的项目是针对.NET Framework 4.6.1

public class ConnectionHandler
{
    private readonly Socket client;

    private readonly IServerRouteConfig serverRouteConfig;

    public ConnectionHandler(Socket client, IServerRouteConfig serverRouteConfig)
    {
        this.client = client;
        this.serverRouteConfig = serverRouteConfig;
    }

    public async Task ProcessRequestAsync()
    {
        string request = await this.ReadRequest();

        IHttpContext context = new HttpContext(request);

        IHttpResponse response = new HttpHandler(this.serverRouteConfig).Handle(context);

        ArraySegment<byte> toBytes = new ArraySegment<byte>(Encoding.ASCII.GetBytes(response.Response));

        await this.client.SendAsync(toBytes, SocketFlags.None);

        Console.WriteLine(request);
        Console.WriteLine(response.Response);

        this.client.Shutdown(SocketShutdown.Both);
    }

1 个答案:

答案 0 :(得分:1)

According to the MSDNSendAsync方法诞生于.NET Framework v3.5,其语法从未改变过:

public bool SendAsync(SocketAsyncEventArgs e)

所以:讲座错了,或者它不是指SendAsync班的Socket方法。

但是,如果您查看Send方法,there's a close-enough overload:

public int Send(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)