简体   繁体   中英

Why is my printf printing the wrong values?

I can't figure out why my code is not working. I am trying to create something similar to P2P file transfer where multiple threads simultaneously grab different parts of a file from a pre-existing server program. The actual problem I am having right now, is much simpler, however.

Since you cannot pass multiple arguments into pthread_create, I created a structure that had the two pieces of information I want to pass. I also created an array of pointers to these structures and initialize each one individually before passing it's pointer in.

printf("In thread: port=%d & ipAddr=%s\n",conn->port,conn->ipAddr);

When that line runs, everything prints out correctly with the correct port number and IP address.

printf("Size of chunk %d received by %lu on port %d: %d bytes\n",chunkNum,pthread_self(),conn->port,sizeRec);

However, when that line runs shortly after, the port number does not print out correctly. Instead of 9210 and 9211, I get 0 and 134520848. Otherwise, everything seems to be working so I'm thinking it may just be a printf problem of some sort, but I want to be sure before I move on to implementing the next part of my project.

If anyone has any idea why the same variable would print with one value and a completely different a few lines later when no changes were made, it would be very helpful for me. I have included all of my code below for reference. Thanks for your help!

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

char * file_name = "output.txt";
int nextChunk = 0;

pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER;

struct connection{
  int port;
  char* ipAddr;
};

void* getFile(void* args) {
  int con_fd = 0;
  int ret = 0;  
  struct sockaddr_in serv_addr;
  struct connection* conn = (struct connection*)args;
  printf("In thread: port=%d & ipAddr=%s\n",conn->port,conn->ipAddr);

memset(&serv_addr, 0, sizeof(struct sockaddr));
serv_addr.sin_family = AF_INET;
//printf("port number: %d\n",conn->port);
serv_addr.sin_port = htons(conn->port);
serv_addr.sin_addr.s_addr = inet_addr(conn->ipAddr);
int sizeRec;
char buf[1024];
while(1) {
    con_fd = socket(PF_INET, SOCK_STREAM, 0);
    if (con_fd == -1) {
        printf("Socket Error\n");
        fflush(stdout);
        return 0;
    }
    ret = connect(con_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr));
    if (ret < 0) {
        printf("Connect error\n");
        fflush(stdout);
        return 0;
    }
    char chunkStr[128];
    pthread_mutex_lock(&lock1);
    int chunkNum = nextChunk++;
    pthread_mutex_unlock(&lock1);
    sprintf(chunkStr,"%d",chunkNum);
    send(con_fd,chunkStr,128,0);
    sizeRec = recv(con_fd,buf,1024,0);
    printf("Size of chunk %d received by %lu on port %d: %d bytes\n",chunkNum,pthread_self(),conn->port,sizeRec);
    if (sizeRec <= 0) {
        return 0;
    }
} 
/*FILE *f = fopen(filename, "w");
if (!f) {
    printf("Can't open %s for input. Program halting\n",filename);
    exit(0);
}*/
/*while ((sizeReceived = recv(sock,buf,1024,0)) > 0) {
    if (fwrite(buf,sizeof(char),sizeReceived,f) == -1) {
        printf("Error writing file");
        exit(0);
    }
}
fclose(f);*/
close(con_fd);
return 0;
}

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

if (argc < 3 || argc % 2 == 0) {
printf("Usage: ./client <ipaddr1> <port1> <ipaddr2> <port2> . . .\n");
return -1;
}
int numThreads = argc / 2;
pthread_t threads[numThreads];
struct connection** connections = malloc(sizeof(struct connection*)*numThreads);
//char* args[numThreads][2];
printf("numThreads: %d\n",numThreads);
for (int i=0; i<numThreads; i++) {
    connections[i] = malloc(sizeof(struct connection));
    connections[i]->ipAddr = argv[2*i+1];
    connections[i]->port = atoi(argv[2*i+2]);
    printf("port number: %d\n",connections[i]->port);
    pthread_create(&threads[i], NULL, getFile, (void*)(connections[i]));
}
for (int i=0; i<numThreads; i++) {
    free(connections[i]);
    pthread_join(threads[i], NULL);
}
pthread_mutex_destroy(&lock1);
return 0;
}

Your main problem is the second for loop in main() .

You first free the data structure and then call pthread_join() . Reverse these two statements and it should work reliable.

If you use Linux, I suggest to use valgrind tool, which easily helps to spot such issues. For windows I only know expensive commercial tools doing the same (like Purify).

Change this :

for (int i=0; i<numThreads; i++) {
    free(connections[i]);
    pthread_join(threads[i], NULL);

To :

for (int i=0; i<numThreads; i++) {
    pthread_join(threads[i], NULL);        
    free(connections[i]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM