Question

Why does this code segment give this output? I need the packet count to increment.

    while(1) {
            if(argv[3]) {
                    strcpy(buf, argv[3]);
            } else {
                    msg = "This is a UDP test string! (packet %d)\n", ++pktcnt;
                    strcpy(buf, msg);
            }

            ret = sendto(udpSocket, buf, strlen(buf)+1, 0, (struct sockaddr*)&udpServer, sizeof(udpServer));
            if(ret == -1) {
                    fprintf(stderr, "Could not send message!\n");
                    return -1;
            } else {
                    printf("Message sent. (packet %d)\n", pktcnt);
            }
            sleep(1);
    }

Output on receiver:

This is a UDP test string! (packet %d)
This is a UDP test string! (packet %d)
This is a UDP test string! (packet %d)

Clearly there is a problem with the format string but I cant figure it out :| I'm sure this is a simple error but I don't know too much C yet, this would work in python!

Was it helpful?

Solution

msg = "This is a UDP test string! (packet %d)\n", ++pktcnt

That is not how you put a formatted string into a variable in C.

You need to use sprintf, and you need to make sure that adequate space is allocated in the string. For example

char msg[200];
sprintf(msg, "This is a UDP test string! (packet %d)\n", ++pktcnt);

Or you can put it directly into buf:

sprintf(buf, "This is a UDP test string! (packet %d)\n", ++pktcnt);

assuming you had enough space there. Remember in C you must allocate space yourself - functions will not do it for you and you will get lots of segmentation faults until you get the message….

OTHER TIPS

msg = "This is a UDP test string! (packet %d)\n", ++pktcnt;

In the above statement, the comma on the lhs is the comma operator. It has the least precedence among all the operators. It evaluates its first operand, then evaluates its second operand and then returns the result of the evaluation of the second operand. There is a sequence point between the evaluation of the first and the second operand. Therefore, the statement is effectively equivalent to

  (msg = "This is a UDP test string! (packet %d)\n"), ++pktcnt;
  |                                                |  |       |
  |________________________________________________|  |_______|
                     |                                    |
                     |                                    |
          assignment expression                     prefix increment

The statement evaluates to the value of prefix increment operator. Therefore, msg always points to the string literal

"This is a UDP test string! (packet %d)\n"

To format the string, you should use the snprintf function.

char buff[100+1]; // +1 for the terminating null byte
msg = "This is a UDP test string! (packet %d)\n"

// write at most sizeof(buff) = 101 bytes into the
// buffer buff including the terminating null byte 
snprintf(buff, sizeof buff, msg, ++pkcnt); 

You are not formatting msg before copying it to buf

msg = "This is a UDP test string! (packet %d)\n", ++pktcnt;
strcpy(buf, msg)

Looks like you were using something like sprintf, but then removed it?

Try something like

sprintf(msg, "This is a UDP test string! (packet %d)\n", ++pktcnt);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top