0

So i have this program where it receives data in a client/server model in linux. I have a circular buffer. I want to store 16384 pieces of data and set a flag after it's filled 16384.

Then return the pointer to that array after it's filled. then moves on to the next chunk and so on. and when my array is full. it starts over. but I don't know how to return the first pointer everytime it changes. I feel like my code logic is wrong.

Any help would be appreciated:

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h> 
#include <stdbool.h>

#define MAX_ITEMS  524288 //nearest power of 2 to half million (2^19)  
typedef struct circularQueue_s
{
    int     first;
    int     last;
    int     validItems;
    int     data[MAX_ITEMS];
} circularQueue_t;

void initializeQueue(circularQueue_t *theQueue);

int isEmpty(circularQueue_t *theQueue);

int putItem(circularQueue_t *theQueue, int theItemValue);

int getItem(circularQueue_t *theQueue, int *theItemValue);

void printQueue(circularQueue_t *theQueue);


void initializeQueue(circularQuete_t *theQueue)
{
    int i;
    theQueue->validItems = 0;
    theQueue->first = 0;
    theQueue->last = 0;
    for (i = 0; i<MAX_ITEMS; i++)
    {
        theQueue->data[i] = 0;
    }
    return;
}


int isEmpty(circularQueue_t *theQueue)
{
    if (theQueue->validItems == 0)
        return(1);
    else
        return(0);
}


//puts new item at the end of the queue, moves rear pointer ahead by 1
int putItem(circularQueue_t *theQueue, int theItemValue)
{
    if (theQueue->validItems >= MAX_ITEMS)
    {
        printf("The queue is full\n");
        printf("You cannot add items\n");
        return(-1);
    }
    else
    {
        theQueue->validItems++;
        theQueue->data[theQueue->last] = theItemValue;
        theQueue->last = (theQueue->last + 1) % MAX_ITEMS;
    }
}


int main(int argc, char *argv[])
{
    int sockfd = 0, n = 0;
    int recvBuff[16384];
    struct sockaddr_in serv_addr;
    circularQueue_t   myQueue;
    initializeQueue(&myQueue);
    bool messageReceivedFlag = false;

    memset(recvBuff, '0', sizeof(recvBuff));
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Error : Could not create socket \n");
        return 1;
    }

    memset(&serv_addr, '0', sizeof(serv_addr));

    serv_addr.sin_addr.s_addr = inet_addr("192.168.100.200");
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(5000);

    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("\n Error : Connect Failed \n");
        return 1;
    }

    int count;
    while ((n = recv(sockfd, recvBuff, sizeof(recvBuff), 0)) > 0)       
    {
        count = 0;

        while (count < 16385)
        {
            putItem(&myQueue, recvBuff[count]);
            count++;
            if (count == 16384) {
                messageReceivedFlag = true; 
            }    
        }    
    }
    printf("then: %d\n", n);

    if (n < 0)
    {
        printf("\n Read error \n");
    }

    return &myQueue;
}
user3078414
  • 1,942
  • 2
  • 16
  • 24

0 Answers0