Here’s my code:
tmpip = get_public_ip();
pubip = strtok(tmpip, "\n");
sprintf(buffer, "220 FTPUtils Server [%s]", pubip);
len_string = strlen(buffer)+1;
if(send(newsockd, &len_string, sizeof(len_string), 0) < 0){
perror("Errore invio len buffer");
onexit(newsockd, sockd, 0, 2);
}
if(send(newsockd, buffer, len_string, 0) < 0){
perror("Errore durante l'invio");
onexit(newsockd, sockd, 0, 2);
}
pubip = NULL;
free(tmpip);
memset(buffer, 0, sizeof(buffer));
if(recv(newsockd, &len_string, sizeof(len_string), MSG_WAITALL) < 0){
perror("Errore ricezione len user buffer");
onexit(newsockd, sockd, 0, 2);
}
if(recv(newsockd, buffer, len_string, 0) < 0){
perror("Errore ricezione del nome utente");
onexit(newsockd, sockd, 0, 2);
}
user_string = strtok(buffer, " ");
username = strtok(NULL, "\n");
fprintf(stdout, "%s %s\n", user_string, username);
sprintf(saved_user, "%s", username);
memset(buffer, 0, sizeof(buffer));
if(recv(newsockd, &len_string, sizeof(len_string), MSG_WAITALL) < 0){
perror("Errore ricezione len pass buffer");
onexit(newsockd, sockd, 0, 2);
}
if(recv(newsockd, buffer, len_string, 0) < 0){
perror("Errore ricezione password");
onexit(newsockd, sockd, 0, 2);
}
pass_string = strtok(buffer, " ");
password = strtok(NULL, "\n");
fprintf(stdout, "%s %s\n", pass_string, password);
memset(buffer, 0, sizeof(buffer));
printf("%s %s\n", username, password);
I have a problem with the last debug printf: it prints to stdout nothing!!
Why are my pointer (username and password) cleared???
It is strange because the 2 fprintf works perfectly but the last printf no…
Both the
usernameand thepasswordpoint to locations insidebuffer. That’s howstrtokworks.In other words, when you zero the buffer using
memsetyou’re zeroing out the memory to whichusernameandpasswordpoint. A simple workaround would be to duplicate that memory:Note
strdupisn’t standard but quite easy to implement usingmallocandstrcpy.