Thread: add sequence number

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    6

    add sequence number

    Hello , im sending a message "teste" by udp socket, i would like to add a sequence number at teste like:

    teste1
    teste2
    teste3
    teste4 and so on....
    i got this peace of code:

    Code:
    #define HELLO_PORT 12345
    #define HELLO_GROUP "192.168.0.1"
    int main(int argc, char *argv[])
    {
    struct sockaddr_in addr;
    int fd;
    char *message = "teste";
    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
    perror("socket");
    exit(1);
    }
    /* set up destination address */
    memset(&addr,0,sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(HELLO_GROUP);
    addr.sin_port=htons(HELLO_PORT);
    while (1)
    {
    if (sendto(fd, message, strlen(message), 0,(struct sockaddr *) &addr, sizeof(addr)) < 0)
    {
    perror("sendto");
    exit(1);
    }
    sleep(2);
    }
    }
    many thanks

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Add a loop counter, and concatenate that value to your string, using sprintf() for instance. What's disturbing you?

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    6
    Quote Originally Posted by root4 View Post
    Add a loop counter, and concatenate that value to your string, using sprintf() for instance. What's disturbing you?
    Im novice to c code , can you add code at code above?

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by davidx View Post
    Im novice to c code , can you add code at code above?
    People on this site are very reluctant to give out code - You have more chance if you look for the function sprintf, give it a go, and then post what you came up with if it doesn't work.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Im novice to c code , can you add code at code above?
    You're messing about with network code. You should be up to the task of creating a string with sprintf()

    But then you're just another found some code merchant

    What happens when you face the next trivial problem due to your lack of knowledge - will you be back on another forum asking for the next simple task.
    Start learning some C.
    C Programming Notes
    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. number sequence fiddling
    By stabu in forum C Programming
    Replies: 6
    Last Post: 03-05-2010, 09:13 AM
  2. Inverting number sequence.
    By Dontgiveup in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2009, 02:15 PM
  3. counting number of integers in sequence
    By rachael033 in forum C++ Programming
    Replies: 1
    Last Post: 05-02-2006, 11:35 PM
  4. Number sequence help
    By Shakti in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-26-2005, 08:36 AM
  5. number sequence
    By valleyboy in forum C Programming
    Replies: 1
    Last Post: 05-01-2005, 02:24 PM