Thread: WSABUF question

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    Post WSABUF question

    Hello..

    I work on a IOCP framework server, where I have to work with WSABUF (on WSARecv and WSASend)..

    I have a special class for buffer which looks like:

    Code:
    class cbuffer {
    	WSABUF m_wsabuf;
    	char m_buf[1024];
    
    void setup() {
    		memset(m_buf, 0, sizeof(m_buf));
    		m_wsabuf.buf = reinterpret_cast<char*>(&m_buf[0]);
    		m_wsabuf.len = 1024 - 1; // - 1 ?
    	}
    };
    I call setup() before I do WSARecv/Send..
    Now I wonder what is the proper/best approach to set wsabuf..
    Right now I memset m_buf and set wsabuf.len to 1023 (as you can see) - so theres a space for the last zero byte.. I wonder if this is the right way to do it?

    All IOCP sources I took a look at set wsabuf.len to the actual size of buffer (so 1024 in this case)..
    Maybe is should do memset(m_buf, 0, sizeof(m_buf) + 1) ?

    My IOCP buffer concept works so that I have a static buffer size (m_buf[1024]), and I do WSARecv until there's data, and each time it gets it, I copy the data to the std::string and call setup(). Do you think this is the "good" way to do it? It seems the best and easier option for me..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > so theres a space for the last zero byte
    Yeah, but that's only if YOU want to store a \0 after the function returns.
    A \0 only makes sense if you're expecting printable text and want an easy way of using other C or C++ string functions.

    There's no problem with setting the buffer size to just sizeof(m_buf), and using the return result to determine how much real data there is.

    > Maybe is should do memset(m_buf, 0, sizeof(m_buf) + 1) ?
    Bad idea - that's a buffer overflow.

    > Do you think this is the "good" way to do it?
    It's a reasonable way to do it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    So actually theres no need to memset m_buf to 0 each time I receive?
    Im not expecting only printable text.. So I guess I dont have to memset it and then just copy bytes I get to the std::string?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > So actually theres no need to memset m_buf to 0 each time I receive?
    Nope.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM