检查SendAsync是否发送完成

发布于 2024-08-15 07:24:52 字数 939 浏览 4 评论 0原文

正如下面的简化代码所示,检查 SendAsync() 是否已完成传输缓冲区中的所有预期字节是正确的做法吗?或者说这是多余的?这是用于 TCP 连接套接字的。我特别担心必须在 ProcessSend() 内对 SendAsync() 发出第二个子调用。

例如,如果我有多个线程使用每个消息的新(池)SocketAsyncEventArg 将数据传输到客户端,那么同时发送是否有可能将其自身插入到部分传输的消息之间?或者,这是通过仅允许在数据发送中使用一个 SocketAsyncEventArg 来控制对客户端的访问的一个很好的理由吗?

private void ProcessSend(SocketAsyncEventArgs e)
{
    // Check that data was sent
    if ((e.SocketError == SocketError.Success) && (e.BytesTransferred > 0))
    {
        AsyncSendUserToken token = (AsyncSendUserToken)e.UserToken;

        // Check that all the data was sent
        int offset = e.Offset + e.BytesTransferred;
        if (offset < e.Count)
        {
            // Call send again to finish the send
            e.SetBuffer(offset, e.Count - offset);
            bool _willRaiseEvent = token.Socket.SendAsync(e);
            if (!_willRaiseEvent)
                ProcessSend(e);
        }
    }
}

As in the simplified code below, is checking that SendAsync() has finished transmitting all intended bytes in the buffer the correct thing to do? Or is that redundant? This is for a TCP connection socket. I am particularly concerned with having to issue a second sub-call to SendAsync() inside ProcessSend().

For example, if I had multiple threads transmitting data to a client using a new (pooled) SocketAsyncEventArg for each message, isn't it possible that a simultaneous send might interject itself in-between a partially transmitted message? Or is this a good reason for controlled access to the client by only allowing one SocketAsyncEventArg to be used in data sending?

private void ProcessSend(SocketAsyncEventArgs e)
{
    // Check that data was sent
    if ((e.SocketError == SocketError.Success) && (e.BytesTransferred > 0))
    {
        AsyncSendUserToken token = (AsyncSendUserToken)e.UserToken;

        // Check that all the data was sent
        int offset = e.Offset + e.BytesTransferred;
        if (offset < e.Count)
        {
            // Call send again to finish the send
            e.SetBuffer(offset, e.Count - offset);
            bool _willRaiseEvent = token.Socket.SendAsync(e);
            if (!_willRaiseEvent)
                ProcessSend(e);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那支青花 2024-08-22 07:24:52

我怀疑您正在阅读 http:// msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx 并看到它们正在检查 ProcessReceive 上的字节长度。

这样做是为了获取所有接收到的字节,因为您无法控制每次从对方发送多少字节。

当您执行发送时,这是多余的,因为框架会为您处理发送所有数据。

I suspect you are reading the example at http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx and see that they are checking the byte length on ProcessReceive.

This is done to in order to get all received bytes, as you have no control on how many bytes are sent from the other party each time.

When you perform a send, this is redundant as the framework handles sending all your data for you.

我还不会笑 2024-08-22 07:24:52

我认为您的代码有问题:

// Check that all the data was sent
int offset = e.Offset + e.BytesTransferred;
if (offset < e.Count)
{

e.Count 是要传输的字节数,它不是字节数组的偏移量。

因此,为了比较苹果与苹果,你会说:

if (e.BytesTransfered < e.Count)
{
}

I think there's something wrong in your code:

// Check that all the data was sent
int offset = e.Offset + e.BytesTransferred;
if (offset < e.Count)
{

e.Count is the number of bytes to transfer, it's not an offset into the byte array.

So, in order to compare apples with apples, you'd say:

if (e.BytesTransfered < e.Count)
{
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文