Thread: How to verify my incoming UDP packet correctly?

  1. #16
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    Code:
    printf( "[%s]", packetBuffer );
    if you don't see [101_idle] but you see [101_idle(+ some other bs)] ;

    that could be your comparison problem.
    Last edited by Structure; 09-11-2020 at 09:30 AM.
    "without goto we would be wtf'd"

  2. #17
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    also i believe when you do this:
    Code:
    packetBuffer[len] = 0;

    you actually want do do this:
    Code:
    packetBuffer[len-1] = 0;

    also, you are going to want to zero terminate both strings.
    Last edited by Structure; 09-11-2020 at 09:31 AM.
    "without goto we would be wtf'd"

  3. #18
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Exclamation

    char situationOne = "_available"

    considering what this code does, this is disturbing.

    How to verify my incoming UDP packet correctly?-badtime-jpg
    Last edited by Structure; 09-11-2020 at 09:40 AM.
    "without goto we would be wtf'd"

  4. #19
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    unknown_072: Your code in #11 still uses "+" for string concatenation. That does not work as laserlight mentioned in post #2. That needs to be fixed.

    Structure: There's no need to reinvent the wheel. Your "compare" function in #5 can be simplified to something like this:

    Code:
    int compare(const char *string1, const char *string2) {
        return strcmp(string1, string2) == 0;
    }
    (I also added "const" to the parameters which you forgot to do.)

  5. #20
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Red face

    which you forgot to do.

    How to verify my incoming UDP packet correctly?-feature-jpg

    How to verify my incoming UDP packet correctly?-nice-jpg
    Last edited by Structure; 09-11-2020 at 09:51 AM.
    "without goto we would be wtf'd"

  6. #21
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    Quote Originally Posted by christop View Post
    Structure: There's no need to reinvent the wheel. Your "compare" function in #5 can be simplified to something like this:

    Code:
    int compare(const char *string1, const char *string2) {
        return strcmp(string1, string2) == 0;
    }
    I can understand this, but why include an entire library for one function?
    Although i used strlen, i'm saying it could be useful sometimes to create the one function you need.
    Last edited by Structure; 09-11-2020 at 09:55 AM.
    "without goto we would be wtf'd"

  7. #22
    Registered User
    Join Date
    Sep 2020
    Posts
    20
    Quote Originally Posted by christop View Post
    unknown_072: Your code in #11 still uses "+" for string concatenation. That does not work as laserlight mentioned in post #2. That needs to be fixed.

    Structure: There's no need to reinvent the wheel. Your "compare" function in #5 can be simplified to something like this:

    Code:
    int compare(const char *string1, const char *string2) {
        return strcmp(string1, string2) == 0;
    }
    (I also added "const" to the parameters which you forgot to do.)
    I checked how to use strcat, but here also, I'm not sure how to read the PacketBuffer, I just want to verify if the 3 digits of the string that is received on the UDP packet are the same as tableID;
    Code:
    if (packetBuffer, strcat(str1, str2)
    Structure:
    I've rewinded my code back to basic, thought it was helpful to share a bit more code.

  8. #23
    Registered User
    Join Date
    Sep 2020
    Posts
    20
    So monday I'll work on this again, first thing, since I really want to finish this piece of code.
    It's clear to me that the "+" does not work like this in C. Hopefully I can work this out with your help.

    Once again, I'm trying to simply read only the first 3 digits of the udp packet and want to compare them to the variable 'tableID'

    Thanks so far!

  9. #24
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Structure View Post
    I can understand this, but why include an entire library for one function?
    You're already linking to libc, right? Then you have all of the string functions.

    Although i used strlen, i'm saying it could be useful sometimes to create the one function you need.
    Sure, it can be useful as an academic exercise, but (IMO) it's pointless if you just want to write an application. Just use existing functions in libc. They're likely better tested and optimized than something you write yourself. Less code means less to debug.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by unknown_072
    I'm sure the message arrives correctly, for debug I did a simple string comparison`!strcmp(packetBuffer, "101_available"));`, which works.
    When I saw that !strcmp(packetBuffer, "101_available")) works but you "Somehow (...) can't get the code correctly to make a correct comparison", and then I see tableID + "_available", it looked like you were trying to do string concatenation using operator + and that's why your comparison failed: you aren't constructing the resulting string correctly. I'm afraid that I didn't notice that you also inexplicably changed from using strcmp to using operator == instead, which would be another reason why your comparison failed: unlike your original test, you aren't comparing strings correctly either. I surmised that this is why Structure suggested a custom comparison function, but as both your own original test and christop noted, strcmp will do.

    Quote Originally Posted by unknown_072
    I checked how to use strcat, but here also, I'm not sure how to read the PacketBuffer, I just want to verify if the 3 digits of the string that is received on the UDP packet are the same as tableID;
    I may have made a mistake in mentioning strcat, even though generally it is a workable approach. In this case, you mentioned that "tableID is defined as byte". Presumably this means something like unsigned char, but you don't want to interpret it as a character; you want to interpret it as an integer string that is prepended to "_available". Hence, a better solution would be to use snprintf:
    Code:
    char expected[14]; // NNN_available
    snprintf(expected, sizeof(expected), "%u_available", (unsigned int)tableID);
    if (strcmp(packetBuffer, expected) == 0) {
        modeAvailable();
    }
    (If you're compiling with respect to pre-C99, you won't be able to use snprintf, but you can still use sprintf. However, then the onus is on you to prevent buffer overflow by checking that tableID really is in the range [0, 1000).)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Sep 2020
    Posts
    20
    Hi laserlight,

    Thank you!
    So I've changed my code:

    I've declared expected

    Code:
          snprintf(expected, sizeof(expected), "%u_available", (unsigned int)tableID);
          
          if (strcmp(packetBuffer, expected) == 0) {
            modeAvailable();
            Serial.println("This works!");
          }
    and declared expected;
    Code:
    char expected[14]; // NNN_available
    And when I upload this I do recieve the message, but it won't return true since I'm not seeing the Serial Print "This Works!"

    This is what I'm seeing in my monitor when sending the packet:

    08:02:05.419 -> Received packet of size 13
    08:02:05.419 -> From 10.168.1.100, port 9101
    08:02:05.419 -> Command: 101_available
    08:02:05.454 -> --END--

    All correct information.
    But still it won't return true

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay. Temporarily change this:
    Code:
    if (strcmp(packetBuffer, expected) == 0) {
      modeAvailable();
      Serial.println("This works!");
    }
    to this:
    Code:
    if (strcmp(packetBuffer, "101_available") == 0) {
      modeAvailable();
      Serial.println("This works!");
    }
    Compile and run your code with exactly the same test input. Do you get "This works!" as the actual output?

    If the answer is yes, then it implies that there's still something wrong with how you're constructing the string. So, one place to start is to insert this before you do the string comparison and see what you get:
    Code:
    Serial.println(expected);
    If the answer is no, then it implies that there's something else wrong, i.e., some other change you made has made your code become incorrect in between the time you wrote that "I'm sure the message arrives correctly, for debug I did a simple string comparison`!strcmp(packetBuffer, "101_available"));`, which works" in post #1 and now.
    Last edited by laserlight; 09-14-2020 at 12:37 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Registered User
    Join Date
    Sep 2020
    Posts
    20
    Quote Originally Posted by laserlight View Post
    Okay. Temporarily change this:
    Code:
    if (strcmp(packetBuffer, expected) == 0) {
      modeAvailable();
      Serial.println("This works!");
    }
    to this:
    Code:
    if (strcmp(packetBuffer, "101_available") == 0) {
      modeAvailable();
      Serial.println("This works!");
    }
    Compile and run your code with exactly the same test input. Do you get "This works!" as the actual output?

    If the answer is yes, then it implies that there's still something wrong with how you're constructing the string. So, one place to start is to insert this before you do the string comparison and see what you get:
    Code:
    Serial.println(expected);
    If the answer is no, then it implies that there's something else wrong, i.e., some other change you made has made your code become incorrect in between the time you wrote that "I'm sure the message arrives correctly, for debug I did a simple string comparison`!strcmp(packetBuffer, "101_available"));`, which works" in post #1 and now.

    Okay so I've tested this, and yes it works.

    09:28:03.457 -> Received packet of size 13
    09:28:03.457 -> From 10.168.1.100, port 9101
    09:28:03.457 -> Command: 101_available
    09:28:03.457 -> --END--
    09:28:03.457 ->
    09:28:03.457 -> Confirmed
    09:28:03.457 -> This works!


    If I add:
    Code:
    Serial.println(expected);
    Nothing is printed, it is a loop so it should print constant

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by unknown_072
    If I add:
    Code:
    Serial.println(expected);
    Nothing is printed, it is a loop so it should print constant
    I wrote a quick program to check that I didn't make some silly mistake:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        unsigned char tableID = 101;
        char expected[14]; // NNN_available
        snprintf(expected, sizeof(expected), "%u_available", (unsigned int)tableID);
        printf("%s\n", expected);
        return 0;
    }
    Compiling and running the above program results in this output, which is exactly what I expected:
    Code:
    101_available
    Therefore, what you have described is impossible (unless there is undefined behaviour elsewhere): there can be no value of tableID that will result in expected being an empty string.

    To confirm, are you saying that you wrote this:
    Code:
    char expected[14]; // NNN_available
    snprintf(expected, sizeof(expected), "%u_available", (unsigned int)tableID);
    Serial.println(expected);
    if (strcmp(packetBuffer, expected) == 0) {
        modeAvailable();
        Serial.println("This works!");
    }
    and it didn't print anything where Serial.println(expected) was supposed to print something?
    Last edited by laserlight; 09-14-2020 at 02:45 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Registered User
    Join Date
    Sep 2020
    Posts
    20
    Quote Originally Posted by laserlight View Post
    I wrote a quick program to check that I didn't make some silly mistake:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        unsigned char tableID = 101;
        char expected[14]; // NNN_available
        snprintf(expected, sizeof(expected), "%u_available", (unsigned int)tableID);
        printf("%s\n", expected);
        return 0;
    }
    Compiling and running the above program results in this output, which is exactly what I expected:
    Code:
    101_available
    Therefore, what you have described is impossible (unless there is undefined behaviour elsewhere): there can be no value of tableID that will result in expected being an empty string.

    To confirm, are you saying that you wrote this:
    Code:
    char expected[14]; // NNN_available
    snprintf(expected, sizeof(expected), "%u_available", (unsigned int)tableID);
    Serial.println(expected);
    if (strcmp(packetBuffer, expected) == 0) {
        modeAvailable();
        Serial.println("This works!");
    }
    and it didn't print anything where Serial.println(expected) was supposed to print something?

    This actually works now! I do have to clean up a bit but this my result from the serial monitor.

    Code:
    11:12:19.369 -> Received packet of size 13
    11:12:19.369 -> From 10.168.1.100, port 9101
    11:12:19.369 -> Command: 215_available
    11:12:19.404 -> --END--
    11:12:19.404 -> 215_available
    11:12:19.404 -> Confirmed
    11:12:19.404 -> This works!
    11:12:19.404 -> Expected: 215_available
    
    
    11:12:25.782 -> Received packet of size 13
    11:12:25.782 -> From 10.168.1.100, port 9101
    11:12:25.782 -> Command: 101_available
    11:12:25.782 -> --END--
    11:12:25.782 -> 215_available
    So, the tableID is currently 215, which is correct.
    With the 101_available it does print 'Expected' which obviously is correct when you look at the code, it's printing what it expects.

    This is what I have;
    Code:
            snprintf(expected, sizeof(expected), "%u_available", (unsigned int)tableID);
            Serial.println(expected);
              if (strcmp(packetBuffer, expected) == 0) 
              {
                modeAvailable();
                Serial.println("This works!");
                Serial.print("Expected: ");
                Serial.println(expected);
                
              }

    I do think this did the trick, I'll work my code out today to see if it works as supposed with all the functions.

    Really thanks for all the help! I'm happy now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constructing a Wake On Lan packet (low level packet)
    By kaptsea in forum C++ Programming
    Replies: 3
    Last Post: 01-28-2020, 07:43 AM
  2. Replies: 1
    Last Post: 11-30-2018, 12:41 AM
  3. Incoming phone call program for C++
    By HARLEY78 in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2010, 05:28 AM
  4. Best way to handle incoming commands from a client.
    By ~Kyo~ in forum Game Programming
    Replies: 6
    Last Post: 07-26-2006, 12:24 PM
  5. changing incoming port for firefox
    By digitaltsai in forum Tech Board
    Replies: 1
    Last Post: 05-05-2006, 09:58 AM

Tags for this Thread