Thread: chat simulation

  1. #1
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Question chat simulation

    Hi iam new to c programming and i have some problems with this program, i would like your help in how about to do this, i want to keep this as simple as possible using only #include<stdio.h>, what i have so far is that it should be done with arrays and a queue, the server will queue all the messages and then send them to the the other clients.

    The Client will randomly generate some text and send it to the Server
    the Server will then echo the text to all Clients including the name of
    the Client sending the message. This will then be displayed by each of
    the Clients that are attached to the Server.
    Server should continuously loop and accept messages from Clients
    and then echo them. Each client gets a unique name which will be passed to
    the server at the time when the connection is established.

    Run one server and ten clients for at least 100 distinct messages from all clients.

    Typical output may look like:
    Client1 sends: abba
    Client3 receives: From Client1: abba
    Client4 receives: From Client1: abba
    etc...

    (remember that more than one message may pile up on one client; therefore, you need
    to allow a queue for each client... similarily, for the server)

    Code:
    1. This is what i have so far can yoU give me some hints. 
    2. Can some one explain how does a queue works with arrays.
    
    Thanks and have a nice day
    
    #include<stdio.h>
    main()
    {
    	client();
    	server();
    	
    	return 0;
    }
    	
    void client(void)		
    	{
    		char client1[5]= "Hello";
    		char client2[5]= "Good";
    		char client3[5]= "abba";
    		char client4[5]= "waooo";
    		char client5[5]= "cool";
    		printf("Client 1 Sends: %s\n", client1);
    	}
    void server(void)
    	{
    		printf("bye\n");
    	}
    Last edited by wise_ron; 11-20-2006 at 01:53 PM.
    Wise_ron
    One man's constant is another man's variable

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    First off, you may want to edit your post...seeing everything in code tags is really weird and awkward...

    As for the Queue thing, a Queue is really just an array with a "front" and a "back". This can be anywhere you want, but typically your front will be the first element, and the back will be the last element. When you enqueue an element, you add it to the end of the list. When you dequeue an element, you remove it from the front of the list.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Unhappy More questions?

    Thanks i have for the advice i edited my post. Can some one explain how to queue a string into array?

    1. What iam thinking is just dealing with two functions one for the client1, that will made of a string and then this string will be store in the server function in a form of a queue fifo, does any know a website that explains queues?
    2. The server funcition will distribute the string from client1 to all the other clients.
    3. Can some one help me improve this code, becuase is not working. Please tell me if iam going in the right path. Thanks for your help.

    Code:
    #include<stdio.h>
    main()
    {
    	client();
    	server();
    	return 0;
    }
    	
    void client(void)		
    	{
    		char client1[5]= "Hello";
    	}
    void server(void)
    	{
    		printf("client1 sends %s", client1);
    		printf("client2 receives:%s", client1);
    		printf("client3 receives:%s", client1);
    		printf("client4 receives:%s", client1);
    		printf("client5 receives:%s", client1);
    		printf("client6 receives:%s", client1);
    		printf("client7 receives:%s", client1);
    		printf("client8 receives:%s", client1);
    		printf("client9 receives:%s", client1);
    		printf("client10 receives:%s", client1);
    	}
    Wise_ron
    One man's constant is another man's variable

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    http://www.nist.gov/dads/#Q

    best link for all your data structure questions

    Code:
    printf("client1 sends %s", client1);
    		printf("client2 receives:%s", client1);
    		printf("client3 receives:%s", client1);
    		printf("client4 receives:%s", client1);
    		printf("client5 receives:%s", client1);
    		printf("client6 receives:%s", client1);
    		printf("client7 receives:%s", client1);
    		printf("client8 receives:%s", client1);
    		printf("client9 receives:%s", client1);
    		printf("client10 receives:%s", client1);
    what is client1? where have u declared that, this program dosn't even compile.

    ssharish2005

  5. #5
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Thumbs up this code is working! :) but not finish

    My problem is implementing in the function client, it suppose to send radomly generate some text and send it to the server function. Can some one give me some hints in how to implement this?

    Thanks for your help.

    Code:
    #include<stdio.h>
    main()
    {
    	client();
    	server();
    	return 0;
    }
    	
    void client(void)		
    	{
    		
    	}
    void server(void)
    	{
    		char client1[]= "Hello";
    		printf("client1  sends: %s\n", client1);
    		printf("client2  receives: %s\n", client1);
    		printf("client3  receives: %s\n", client1);
    		printf("client4  receives: %s\n", client1);
    		printf("client5  receives: %s\n", client1);
    		printf("client6  receives: %s\n", client1);
    		printf("client7  receives: %s\n", client1);
    		printf("client8  receives: %s\n", client1);
    		printf("client9  receives: %s\n", client1);
    		printf("client10 receives: %s\n", client1);
    	}
    Wise_ron
    One man's constant is another man's variable

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You should :
    1) Create Random Text
    2) Enqueue to Queue.

    For your client :

    1) Dequeue an element from the queue.
    2) Send it.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could implement an efficient queue in terms of an array, using a circular buffer. A well-implemented circular buffer would allow for huge event queues up into the thousands and is expressly designed for this purpose.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Question How do i do queues with an array of string?

    Hi everyone thanks for your comments, i understand the logic of the program but when it comes to the actual work i dont have the minimum idea in how to implement a queue with an array or a circular buffer i have done some research online but i havent found any simple code example that will help me to understand this topic. Can some one give me another site or explain it to me?

    Thanks for your time and help. Have a nice day!
    Wise_ron
    One man's constant is another man's variable

  9. #9
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok, you'll have to be more specific. I believe my first post gave you a brief and easy overview of how to implement a queue with an array.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  10. #10
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    how i do a queue?

    can you give me any code example in how a queue works with strings? I understand the logic you give me but how to do it in actual code is my problem? do you have any example similar to the one you gave me or something similar to this problem?

    Thanks
    Wise_ron
    One man's constant is another man's variable

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int queue[MAX], top = 0, bot = 0;
    
    void push(int queue[], int n, int max, int *top, int *bot) {
        queue[*top] = 0;
        if((*top) ++ == max) *top = 0;
    }
    
    /* ... */
    Does that help?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    is there any code that can be done with * pointers

    I prefer not to use pointer just a string array can that be done? implementing the queue, without pointers.
    Wise_ron
    One man's constant is another man's variable

  13. #13
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    How are you going to do an array of strings without pointers ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  14. #14
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    an array of arrays

    Can it be done using array of arrays? instead of pointers?

    Thanks for your help, have a nice day
    Wise_ron
    One man's constant is another man's variable

  15. #15
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I don't see why you want to avoid pointers so bad...

    If you're going to do queues, you'll have to learn them sooner or later.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with time simulation assignment
    By rakan in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2006, 11:39 AM
  2. Requesting Java Chat Client Maintenence
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-02-2003, 01:57 PM
  3. Chat server/client trial -- anyone interested?
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-23-2003, 10:47 PM
  4. SO close to finishing chat program...!
    By Nakeerb in forum C++ Programming
    Replies: 13
    Last Post: 10-26-2002, 12:24 PM
  5. Rough Portable Chat Design Sketch
    By ggs in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-27-2001, 07:44 AM