Thread: Simulation of a server and clients

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

    Question Simulation of a server and clients

    Hi everyone, Iam new in C programming and I need some help solving this program, this dosent have to do anything with network programming. This is just basically a C language simulation program between clients and a server. There is a function for server and a function for clients that i need to do... this is where iam stuck i need to apply queues but i need to know where? can some one give me hints...

    this is the program requirements are:
    Code:
    In c programming create a Server process and a Client process. 
    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... smile
    
    or... if you wish...
    Client1 sends: abba
    Client3 receives: Client1> abba
    Client4 receives: Client1> abba
    etc... smile
    
    (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)
    This is what i have so far iam in stage 0.5 right now. Can some of you give me some hints in how to continue develop this program. Thank you very much

    Code:
    #include<stdio.h>
    main()
    //void client;
    {
      char message[30];
      printf("THE SIMULATION OF A CLIENT AND SERVENT PROGRAM\n\n");
      printf("Client1 Sends: ");
      scanf("%s",&message);
      printf("Client2 Receives: %s",message);
    
      //void client()
    			//{
    
    
    	         //}
      
      //void server()
    			//{
    
    
    	         //}
    	
    return 0;
    }
    Last edited by wise_ron; 10-07-2006 at 10:34 PM.
    Wise_ron
    One man's constant is another man's variable

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Search google about Winsocks.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well as you're doing a simulation of a network rather than using a real network...

    > this is where iam stuck i need to apply queues but i need to know where?
    How about between the client and the server?

    Before you even do any of that, just read up on queues and how they work (plenty of examples on this forum).

    Each client has an input queue, and the server has an input queue.
    sending a message writes a message to the input queue of the thing you're sending to
    receiving a message reads a message from your own input queue.

    Most important of all, build it up in stages. Don't try and write the whole thing in one go.
    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.

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

    Lightbulb Stage 1 - doing queues

    Iam new in C but this is what i have so far. This program is a simulation it dosent have to be real. This program is to get the concept between clients and servers and how they work. I need some help with this code i want to make it as simple as posible. Please feel free to give some comments in how to improve this code.

    Thanks

    Code:
    #include <stdio.h>   // in/out function header
    #include <string.h>  // string function header
    #include <stdlib.h>  // malloc()
    
    void enter(void);
    void list(void);
    void q_store(char *ptr);
    int store = 0;         // next storage position in queue[] 
    int retrieve = 0;      // retrieve position in queue[] 
    char *queue[100];      // this array forms the queue 
    
    void main(void)
    {
      enter();     // enter some data in the queue and list the data 
      puts("\n\nClient Sends Messages:");
      list();   
      getchar();
    }
    
    //
    // prompt for data entry and store in queue via q_store()
    //
    void enter(void) 
    {
      static char str[100], *ptr;
    
      do {
        printf("Enter message (ENTER only will exit) : ");
        gets(str);
        ptr = (char *) malloc(strlen(str));  // starting address of memory 
        strcpy(ptr,str);
        if (*str)  
          q_store(ptr);   // store in queue if string has info 
      } while (*str);     // until no entry
    }
    
    void list(void)   // list the contents of the queue 
    {
      int k;
    
      for (k = retrieve; k < store; k++)
      printf("Client Receives:%d) %s\n",k+1,queue[k]);
    }
    
    //
    // store data items in the queue 
    //
    void q_store(char *ptr)
    {   
      if (store == 100) { 
        puts("\nList is full!");  
        return; 
      }
      queue[store] = ptr;
      store++;  // point to next available storage position in queue[] 
    }
    Wise_ron
    One man's constant is another man's variable

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int store = 0; // next storage position in queue[]
    > int retrieve = 0; // retrieve position in queue[]
    > char *queue[100]; // this array forms the queue
    Put these things into a queue structure, so you can write things like
    addToQueue( &myQueue, myString );

    You will need multiple queues, so it makes sense to parameterise your queue functions to be able to add to any instance of a queue.

    > ptr = (char *) malloc(strlen(str));
    1. Don't cast malloc in C programs, see the FAQ
    2. Don't forget to count the \0 as well when allocating space for strings.
    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. Communicating clients that are connected to the same server
    By seforix in forum Networking/Device Communication
    Replies: 6
    Last Post: 05-29-2009, 08:13 PM
  2. Can't get to connect clients to server
    By pyngz in forum C# Programming
    Replies: 2
    Last Post: 03-11-2009, 04:46 AM
  3. Multiple clients to "internet" server
    By Zarniwoop in forum C Programming
    Replies: 2
    Last Post: 10-11-2008, 11:04 PM
  4. TCP Sockets: multiple clients - one server
    By Printisor in forum C Programming
    Replies: 4
    Last Post: 11-01-2007, 10:34 AM
  5. MISO Soup: Multiple Clients and one Server
    By doubleanti in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-24-2007, 02:29 AM