1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Utopian implementation help

Discussion in 'C' started by imported_compEng, Apr 22, 2011.

  1. imported_compEng

    imported_compEng New Member

    Joined:
    Apr 22, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    i have a c code Utopian protocol that i need to implement by reading from a file:
    typedef enum {frame_arrival} event_type;
    Code:
    #include "protocol.h"
    #include <stdio.h>
    void sender1(void)
    {
    
      frame s;	
      packet buffer;	
    int i;
      while (true) {
    	  for(i=0; i<=3; i++)
    	  {
    		buffer.data[i]= read(i);
    	  }
            from_network_layer(&buffer);	
            s.info = buffer;	
            to_physical_layer(&s);	
      }	
    }
    
    void receiver1(void)
    {
      frame r;
      event_type event;	
      while (true) {
            wait_for_event(&event);	
            from_physical_layer(&r);	
            to_network_layer(&r.info);	
      }
    }
    
    
    char read(int a)
    {
    	FILE *fp;//local pointer of type file
    	char ch;
    	int j;
    	fp= fopen("mytest.txt", "r");
    	for(j=0; j<=3; j++)
    	{
    	ch = fgetc( fp );
    	if (j==a)
    		return ch;
    }}
    until now i am trying to implement first of all to read from a file read(int a) and put the result in the struct buffer of type that has inside it char data[4]. but unfortunatly i get htis error Error 2 error C2371: 'read' : redefinition; different basic types c:\protocolsc\p1.c 46
    can anyone help me and give me a clue how to implement this code please rather than using files to check if sender and reciever works...thanksss!!!
     
  2. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    1) read() is a c library function - use some other name say protoread() for your function

    2) declare the prototype : char protoread(int a); (and for other functions as well) at the top of your file after all your '#include's and 'typedef's if any
     
  3. imported_compEng

    imported_compEng New Member

    Joined:
    Apr 22, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    i have changed read-->protoRead as you said, but why shell i declare the functions twice??non need to declare them only once as i did...u tried it anyway it gave me more erorrs, so i changed to original and only changed read to protoRead but still same error Error 2 error C2371: 'protoRead' : redefinition; different basic types c:\protocolsc\p1.c 44
    i have included protocol .h if it may help to descover where is my mistake here it is:

    #define MAX_PKT 4 /* determines packet size in bytes */
    /*The typedef keyword allows the programmer to create new names for types such as int, syntax for defining new type */
    typedef enum {false, true} boolean; /* boolean type ... value of enum usually start with zero, enum
    Defines a set of constants of type int*/
    typedef unsigned int seq_nr; /* sequence or ack numbers... unsigned means range is from 0 to 65535 */
    typedef struct {unsigned char data[MAX_PKT];} packet; /* packet definition */
    typedef enum {data, ack, nak} frame_kind; /* frame_kind definition */

    typedef struct { /* frames are transported in this layer...typedef provide mechanism for
    creating synonyms for previously defined data types(struct)which is frame below */
    frame_kind kind; /* what kind of a frame is it? */
    seq_nr seq; /* sequence number */
    seq_nr ack; /* acknowledgement number */
    packet info; /* the network layer packet */
    } frame;

    /* Wait for an event to happen; return its type in event. */
    void wait_for_event(event_type *event);

    /* Fetch a packet from the network layer for transmission on the channel. */
    void from_network_layer(packet *p);

    /* Deliver information from an inbound frame to the network layer. */
    void to_network_layer(packet *p);

    /* Go get an inbound frame from the physical layer and copy it to r. */
    void from_physical_layer(frame *r);

    /* Pass the frame to the physical layer for transmission. */
    void to_physical_layer(frame *s);

    /* Start the clock running and enable the timeout event. */
    void start_timer(seq_nr k);

    /* Stop the clock and disable the timeout event. */
    void stop_timer(seq_nr k);

    /* Start an auxiliary timer and enable the ack_timeout event. */
    void start_ack_timer(void);

    /* Stop the auxiliary timer and disable the ack_timeout event. */
    void stop_ack_timer(void);

    /* Allow the network layer to cause a network_layer_ready event. */
    void enable_network_layer(void);

    /* Forbid the network layer from causing a network_layer_ready event. */
    void disable_network_layer(void);

    /* Macro inc is expanded in-line: Increment k circularly... ex: inc(b) it will be replaced by the following statement */
    #define inc(k) if (k < MAX_SEQ) k = k + 1; else k = 0
    it is the header that i included on the top of my code i just need to implement the sender and reciever in my code that i wrote before so i used reading from a file then later i need to write on to a file from the reciever but firat i need to test reading a file to make it work then writing....please help this was the original code befora i changed it:

    /* Protocol 1 (utopia) provides for data transmission in one direction only, from
    sender to receiver. The communication channel is assumed to be error free,
    and the receiver is assumed to be able to process all the input infinitely fast.
    Consequently, the sender just sits in a loop pumping data out onto the line as
    fast as it can. */

    typedef enum {frame_arrival} event_type;
    #include "protocol.h"
    #include <stdio.h>

    void sender1(void)
    {

    frame s; /* buffer for an outbound frame */
    packet buffer; /* buffer for an outbound packet */
    while (true) {
    from_network_layer(&buffer); /* go get something to send */
    s.info = buffer; /* copy it into s for transmission */
    to_physical_layer(&s); /* send it on its way */
    } /* tomorrow, and tomorrow, and tomorrow,
    Creeps in this petty pace from day to day
    To the last syllable of recorded time;
    - Macbeth, V, v */
    }

    void receiver1(void)
    {
    frame r;
    event_type event; /* filled in by wait, but not used here */

    while (true) {
    wait_for_event(&event); /* only possibility is frame_arrival */
    from_physical_layer(&r); /* go get the inbound frame */
    to_network_layer(&r.info); /* pass the data to the network layer */
    }
    }
    please compare the code that i wrote and look at the protocol.h with the original code and help me to know if i am going on the right way..thanks alot
     
  4. imported_compEng

    imported_compEng New Member

    Joined:
    Apr 22, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Utopian protocol implementation help

    I have this original code that I need to implement it(test if it works) so I chose that the best way to test is reading and writing from a file: (first I am testing to read from a file) in the sender function below but still not working!! I need to know if I am going on the right way and if any help on how to make this code work with reading and writing to a file …
    This is the original code:
    /* Protocol 1 (utopia) provides for data transmission in one direction only, from
    sender to receiver. The communication channel is assumed to be error free,
    and the receiver is assumed to be able to process all the input infinitely fast.
    Consequently, the sender just sits in a loop pumping data out onto the line as
    fast as it can. */

    typedef enum {frame_arrival} event_type;
    #include "protocol.h"
    #include <stdio.h>

    void sender1(void)
    {

    frame s; /* buffer for an outbound frame */
    packet buffer; /* buffer for an outbound packet */
    while (true) {
    from_network_layer(&buffer); /* go get something to send */
    s.info = buffer; /* copy it into s for transmission */
    to_physical_layer(&s); /* send it on its way */
    } /* tomorrow, and tomorrow, and tomorrow,
    Creeps in this petty pace from day to day
    To the last syllable of recorded time;
    - Macbeth, V, v */
    }

    void receiver1(void)
    {
    frame r;
    event_type event; /* filled in by wait, but not used here */

    while (true) {
    wait_for_event(&event); /* only possibility is frame_arrival */
    from_physical_layer(&r); /* go get the inbound frame */
    to_network_layer(&r.info); /* pass the data to the network layer */
    }
    }
    And the header protocol that is included at the top is as following:

    #define MAX_PKT 4 /* determines packet size in bytes */
    /*The typedef keyword allows the programmer to create new names for types such as int, syntax for defining new type */
    typedef enum {false, true} boolean; /* boolean type ... value of enum usually start with zero, enum
    Defines a set of constants of type int*/
    typedef unsigned int seq_nr; /* sequence or ack numbers... unsigned means range is from 0 to 65535 */
    typedef struct {unsigned char data[MAX_PKT];} packet; /* packet definition */
    typedef enum {data, ack, nak} frame_kind; /* frame_kind definition */

    typedef struct { /* frames are transported in this layer...typedef provide mechanism for
    creating synonyms for previously defined data types(struct)which is frame below */
    frame_kind kind; /* what kind of a frame is it? */
    seq_nr seq; /* sequence number */
    seq_nr ack; /* acknowledgement number */
    packet info; /* the network layer packet */
    } frame;

    /* Wait for an event to happen; return its type in event. */
    void wait_for_event(event_type *event);

    /* Fetch a packet from the network layer for transmission on the channel. */
    void from_network_layer(packet *p);

    /* Deliver information from an inbound frame to the network layer. */
    void to_network_layer(packet *p);

    /* Go get an inbound frame from the physical layer and copy it to r. */
    void from_physical_layer(frame *r);

    /* Pass the frame to the physical layer for transmission. */
    void to_physical_layer(frame *s);

    /* Start the clock running and enable the timeout event. */
    void start_timer(seq_nr k);

    /* Stop the clock and disable the timeout event. */
    void stop_timer(seq_nr k);

    /* Start an auxiliary timer and enable the ack_timeout event. */
    void start_ack_timer(void);

    /* Stop the auxiliary timer and disable the ack_timeout event. */
    void stop_ack_timer(void);

    /* Allow the network layer to cause a network_layer_ready event. */
    void enable_network_layer(void);

    /* Forbid the network layer from causing a network_layer_ready event. */
    void disable_network_layer(void);

    /* Macro inc is expanded in-line: Increment k circularly... ex: inc(b) it will be replaced by the following statement */
    #define inc(k) if (k < MAX_SEQ) k = k + 1; else k = 0

    Ok now my code that I reached till now is as following:
    /* Protocol 1 (utopia) provides for data transmission in one direction only, from
    sender to receiver. The communication channel is assumed to be error free,
    and the receiver is assumed to be able to process all the input infinitely fast.
    Consequently, the sender just sits in a loop pumping data out onto the line as
    fast as it can. */

    typedef enum {frame_arrival} event_type;
    #include "protocol.h"
    #include <stdio.h>

    void sender1(void)
    {

    frame s; /* buffer for an outbound frame */
    packet buffer; /* buffer for an outbound packet */
    int i;
    while (true) {
    for(i=0; i<=3; i++)
    {
    buffer.data= protoRead(i);
    }
    from_network_layer(&buffer); /* go get something to send */
    s.info = buffer; /* copy it into s for transmission */
    to_physical_layer(&s); /* send it on its way */
    } /* tomorrow, and tomorrow, and tomorrow,
    Creeps in this petty pace from day to day
    To the last syllable of recorded time;
    - Macbeth, V, v */
    }

    void receiver1(void)
    {
    frame r;
    event_type event; /* filled in by wait, but not used here */

    while (true) {
    wait_for_event(&event); /* only possibility is frame_arrival */
    from_physical_layer(&r); /* go get the inbound frame */
    to_network_layer(&r.info); /* pass the data to the network layer */
    }
    }

    char protoRead(int a)
    {
    FILE *fp;//local pointer of type file
    char ch;
    int j;
    fp= fopen("mytest.txt", "r");//to establish a link btwn program and os open the file in read mode
    for(j=0; j<=3; j++)
    {
    ch = fgetc( fp );/*reads the character from current pointer position, advances the pointer position so that it now points to the next character,
    and returns the character that is read, which we collected in the variable ch*/
    if (j==a)
    return ch;
    }
    }

    But I still have an error as following Error 2 error C2371: 'protoRead' : redefinition; different basic types c:\protocolsc\p1.c 44…
    I need help if anyone can correct what I am doing and help me in how to write to a file from the receiver(void) it will be a great pleasure
     
  5. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    Please don't create new thread for same topic and I have merged it for you.
     
  6. imported_compEng

    imported_compEng New Member

    Joined:
    Apr 22, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    ok i saw that... thanks, but can i delete old threads and write new?how?
     
  7. imported_compEng

    imported_compEng New Member

    Joined:
    Apr 22, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    i still have the same trouble please check it for me here is more details thanks
     

Share This Page