Thread: jumble socket data

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    29

    jumble socket data

    I have some data I am wanting to jumble and send over a socket.
    All I want to do is add 4 random characters every 4 spaces in the the_data variable.
    Can anyone here point me toward some functions or examples that might help me out?

    -thanks in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For what part? Jumbling? Write your own. For sending over a socket? Consider reading Beej's guide. I'm sure your nearest search engine will have a ton of info.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    29
    Quote Originally Posted by quzah
    For what part? Jumbling? Write your own. For sending over a socket? Consider reading Beej's guide. I'm sure your nearest search engine will have a ton of info.


    Quzah.
    Okay well what I mean is I have no idea how to add 4 random characters every space. I already have that guide and know how to use sockets.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Just write a couple of loops to tackle the problem step by step.

    1. Read 4 characters from the source string into a destination string
    2. Generate 4 random characters and add them to the destination string
    3. Repeat until the string ends

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    29
    Yes, but I'm pretty new to C. The problem isn't planning it out, what are some functions I can man to help me do this?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > what are some functions I can man to help me do this?

    rand(); - a random number
    rand()%26; - a random number in a range
    rand()%26+'A'; - a random letter.

    Now wrap that in a for loop to generate an array of random letters

    Now wrap that loop up in a handy function which will
    - make the 4 random letters
    - send it on the socket of your choice.
    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.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    29
    thank you very much

  8. #8
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    // Read in a string and add four random letters 
    // Every four letters in that string
    
    #include <iostream>
    #include <string>
    
    int GetRand(int min, int max);
    
    using namespace std;
    
    int main()
    {
        char array[100] = "Hello everyone this is my string unblemished";
        char letters[30]= "abcdefghijklmnopqrstuvwxyz";
        
        int size = strlen(array);
        
        for (int i=0; i<size; i++)
        {
            if(i%4==0)
            {
                for (int j=0; j<4; j++)
                {
                int r;
                r = GetRand(0, 25);
                cout<<letters[r];
                }
            }
            
                cout<<array[i];
            
        }
        cin.get();
        return 0;
    }                        
                
            
            
            
    /*=============================
      Random function definition
      =============================*/
    
    int GetRand(int min, int max)
    {
      static int Init = 0;
      int rc;
      
      if (Init == 0)
      {
        /*
         *  As Init is static, it will remember it's value between
         *  function calls.  We only want srand() run once, so this
         *  is a simple way to ensure that happens.
         */
        srand(time(NULL));
        Init = 1;
      }
    
      /*
       * Formula:  
       *    rand() % N   <- To get a number between 0 - N-1
       *    Then add the result to min, giving you 
       *    a random number between min - max.
       */  
      rc = (rand() % (max - min + 1) + min);
      
      return (rc);
    }
    This is c++ but you get the idea. I hope this isn't h/w btw

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by treenef
    Code:
    char letters[30]= "abcdefghijklmnopqrstuvwxyz";
    This is c++ but you get the idea. I hope this isn't h/w btw
    Apparently the Alphabet when used in C++ has 30 letters...


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Yeah I just did that as a fail safe. Normally you have to declare it one more than the actual contents to account for the null terminator, I just went a little overboard. HO ho.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by treenef
    Normally you have to declare it one more than the actual contents to account for the null terminator
    Let the compiler do the thinking:
    Code:
    char letters[]= "abcdefghijklmnopqrstuvwxyz";
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting a struct for sending to socket
    By chrisjmoss in forum Networking/Device Communication
    Replies: 6
    Last Post: 04-08-2008, 09:11 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Replies: 6
    Last Post: 12-27-2007, 11:10 AM
  4. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  5. Client-Server Data Read Problem
    By bob2509 in forum C Programming
    Replies: 8
    Last Post: 11-06-2002, 11:47 AM