Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8064227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:18:49+00:00 2026-06-05T11:18:49+00:00

I have some questions about when it is needed to store the data in

  • 0

I have some questions about when it is needed to store the data in the wait buffer (waiting for the FD_WRITE event).

This is my send function (fixed):

bool MyClass::DataSend(char *buf, int len)
{
    if (len <=0 || m_Socket == INVALID_SOCKET) return false;

    if (m_SendBufferLen > 0)
    {
        if ((m_SendBufferLen + len) < MAX_BUFF_SIZE)
        {
            memcpy((m_SendBuffer + m_SendBufferLen), buf, len);
            m_SendBufferLen += len;
            return true;
        }
        else
        {
            // close the connection and log insuficient wait buffer size
            return false;
        }
    }

    int iResult;
    int nPosition = 0;
    int nLeft = len;

    while (true)
    {
        iResult = send(m_Socket, (char*)(buf + nPosition), nLeft, 0);

        if (iResult != SOCKET_ERROR)
        {
            if (iResult > 0)
            {
                nPosition   += iResult;
                nLeft       -= iResult;

            }
            else
            {
                // log 0 bytes sent
                break;
            }
        }
        else
        {
            if (WSAGetLastError() == WSAEWOULDBLOCK)
            {
                if ((m_SendBufferLen + nLeft) < MAX_BUFF_SIZE)
                {
                    // log data copied to the wait buffer
                    memcpy((m_SendBuffer + m_SendBufferLen), (buf + nPosition), nLeft);
                    m_SendBufferLen += nLeft;
                    return true;
                }
                else
                {
                    // close the connection and log insuficient wait buffer size
                    return false;
                }
            }
            else
            {
                // close the connection and log winsock error
                return false;
            }
        }

        if (nLeft <= 0) break;
    }

    return true;
}

My send (FD_WRITE event) function (fixed):

bool MyClass::DataSendEvent()
{
    if (m_SendBufferLen < 1) return true;

    int iResult;
    int nPosition = 0;
    int nLeft = m_SendBufferLen;

    while (true)
    {
        iResult = send(m_Socket, (char*)(m_SendBuffer + nPosition), nLeft, 0);

        if (iResult != SOCKET_ERROR)
        {
            if (iResult > 0)
            {
                nPosition   += iResult;
                nLeft       -= iResult;
            }
            else
            {
                // log 0 bytes sent
                break;
            }
        }
        else
        {
            if (WSAGetLastError() == WSAEWOULDBLOCK)
            {
                if (nPosition > 0)
                {
                    memmove(m_SendBuffer, (m_SendBuffer + nPosition), (m_SendBufferLen - nPosition));
                    m_SendBufferLen -= nPosition;
                }

                break;
            }
            else
            {
                // close the connection and log winsock error
                return false;
            }
        }

        if (nLeft <= 0)
        {
            if (m_SendBufferLen == nPosition)
            {
                m_SendBufferLen = 0;
                break;
            }
            else
            {
                memmove(m_SendBuffer, (m_SendBuffer + nPosition), (m_SendBufferLen - nPosition));
                m_SendBufferLen -= nPosition;
                nPosition   = 0;
                nLeft       = m_SendBufferLen;
            }
        }
    }

    return true;
}

Do I really need the if (nPosition > 0) or not? How do I simulate this scenario? Is there possible send() in non-blocking mode send less bytes than the requested? If not, why using the while() looping?

  • This is the final code (thanks to @user315052)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:18:50+00:00Added an answer on June 5, 2026 at 11:18 am

    At the top of your while loop, you are already decrementing nLeft, so you don’t need to subtract nPosition from it.

    iResult = send(m_Socket, (char*)(buf + nPosition), nLeft, 0);
    

    In your second function, when you are shifting the unsent bytes to the beginning of the array, you should use memmove, since you have overlapped regions (you are copying a region of m_SendBuffer into itself). The overlap is illustrated below, where some of the A‘s would get copied onto itself.

    m_SendBuffer: [XXXXAAAAAAAAAAAA]
    mPosition: 4
    nLeft: 12
    

    I am a little confused about why DataSend() is implemented to allow the caller to keep calling it with success even when the WSAEWOULDBLOCK is encountered. I would suggest the interface be modified to return a result that lets the caller know that it should stop sending, and to wait for an indication to resume sending.

    You don’t need the nPosition > 0 check.

    You can force the case to occur by having the receiver of the data not read anything.

    It is definitely possible for send in non-blocking mode to send fewer bytes than requested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some questions about the default values in a function parameter list Is
I have some questions about the performance of this simple python script: import sys,
i have some questions about constructors in ColdFusion : must i use the name
I have some questions about using MySQLi queries, and related memory management. Suppose I
I have some questions about vector in STL to clarify..... Where are the objects
I have some questions about the registry. We have Preferences p = Preferences.userRoot(); If
I am using Tomcat 6 and have some questions about Apache mod_jk as follows.
I am currently using Visual Studio Express C++ 2008, and have some questions about
I'm brand new to SQL Server 2008, and have some newbie questions about the
I have some basic (stupid) questions about AWS EC2 instances or AMIs. I created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.