Checking if buffer is completely empty...

This is a discussion on Checking if buffer is completely empty... within the C Programming forums, part of the General Programming Boards category; Hey guys. I figured out a way to organize the UI for the ECS program I am working on. After ...

  1. #1
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Checking if buffer is completely empty...

    Hey guys. I figured out a way to organize the UI for the ECS program I am working on. After I recv() the message from the socket into buff, I could check if it received any message(if the buffer is not completely empty). if it id, then decrypt the message and then show it to the user. But if the buffer is empty, then don't print anything at all on the screen. Because right now, if the message is empty, it still prints "Message from client: " without the message

    here is the code:
    Code:
                    if(frkserv > 0)
                    {
                        if(recv(clisock, &buff, sizeof(buff), 0) != NULL)
                        {
                            if(buff != ' ')
                            {
                                ecsdecrypto(buff);
                                printf("Message from client: %s\n", buff);
                            }
                        }             
                    }
    Just part of the code that I am talking about. I need to replace if(buff != ' ') with another condition. I need to check if there is anything in the buffer. I could do it with a loop through all the elements of the char[] and check, but I thought there was a function to do it.
    <<deleted because of colour and size abuse>>

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Maybe by checking the proper return result of recv(), which does NOT return a pointer.

    If it's non-blocking, you should be expecting a return result of zero, AND errno set to EAGAIN.
    Meaning, nothing at the moment (=0), and try again later.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53
    I knew recv() would wait until it a message was available on a socket. That's why i was gonna check the string instead of the recv.
    <<deleted because of colour and size abuse>>

  4. #4
    FOSS Enthusiast
    Join Date
    Jun 2008
    Location
    Germany
    Posts
    64
    either strcmp() or loop through the string. I don't think there's another way.
    And I'd prefer the latter one, if you want to filter strings like " ", " ", etc.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    If the socket is blocking, there's no such thing as a completely empty buffer. You're always going to get at least 1 char.

    What you might have is a not completely filled buffer, but that's something you need to deal with yourself.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53
    Thanks. I just did if(recv(clisock, &buff, sizeof(buff), 0) != 0 && recv(clisock, &buff, sizeof(buff), 0) != 0)


    Thank you very much salem.
    <<deleted because of colour and size abuse>>

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    That works?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53
    Yes. Thank You! Now, all I have trouble with is the prompt to send a new message to the server. I gotta find a way to fix it. If I have any major trouble. I will ask for help in this thread! It's related!
    <<deleted because of colour and size abuse>>

  9. #9
    FOSS Enthusiast
    Join Date
    Jun 2008
    Location
    Germany
    Posts
    64
    I doubt this works. This will read two messages and discard the first one. Later on, this will definitely lead to unwanted behaviour

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Frame buffer not working
    By Noise in forum Game Programming
    Replies: 1
    Last Post: 02-15-2009, 11:05 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  3. Print out a buffer
    By SwarfEye in forum C Programming
    Replies: 4
    Last Post: 09-08-2006, 09:32 AM
  4. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21