Matthew Snyder
Matthew Snyder

Reputation: 1

C-threaded program: Why is this code giving me the "redeclared as different kind of symbol" error?

I am trying to write a client program that will download a file from a server in multiple chunks at a time using multiple threads. I am first trying to successfully download the file using only one thread before moving on, but I keep getting this redeclaration error. I have declared a global variable, index, and want to update it within a function.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define FILE_NAME "output.txt"

int index = 0;
FILE * file;

void * receiveChunk(void * arg){

    int sockfd = 0;
    char buffer[1024];
    char request[128];
    int status = -1;
    char ** hostInfo = (char **) arg; //might need three stars
    struct sockaddr_in serv_addr;
    int port = atoi(*(hostInfo + 2));
    int connection_status;

    while(status != 0){

    memset(&serv_addr, 0, sizeof(struct sockaddr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(*(hostInfo + 1));
    serv_addr.sin_port = htons(port);

    sockfd = socket(PF_INET,SOCK_STREAM,0);
    connection_status = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));

    if(connection_status == -1){
        printf("Error connecting to socket\n");
    }

    file = fopen(FILE_NAME, "w");

    sprintf(request, "%d", index);
    send(sockfd, request, sizeof(request), 0);
    index++;

    recv(sockfd, &buffer, sizeof(buffer), 0);

    fwrite(buffer, 1, status, file);

    }

    return NULL;


}

int main(int argc, int ** argv){

    pthread_t thread_id;
    pthread_create(&thread_id, NULL, &receiveChunk, argv);
    pthread_join(thread_id, NULL);

return 0;
}

As far as I can tell, I have not redeclared index within either function and I thought that in C you can modify global variables within functions with no problems. I'm very sorry if this is some obvious error or misunderstanding, but any help would be greatly appreciated.

Upvotes: 0

Views: 162

Answers (0)

Related Questions