Trying to download a binary file from HTTP server
Zaita, about the same. Not grabbing/writing all of the data.
Quant, can you please reword that? I'm not quite sure what you're asking.
I would try something like this for your receive loop if you're still getting partial data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
char *data = response;
int num_tries = 0;
bool exit_loop = false;
while(!exit_loop)
{
r = recv(sockfd, buffer, 128, 0);
if (r > 0)
{
memcpy(data, buffer, r);
data += r;
memset(buffer, NULL, 128);
num_tries = 0;
printf("Received data - Buffer size: %d, Total response size: %d\r\n",
r, sizeof(data - response));
}
else if (num_tries == 4)
{
exit_loop = true;
printf("Timeout reached, no more data\n");
}
else
{
num_tries++;
sleep(1);
printf("No data read on attempt %d\n", num_tries);
}
}
| |
In answer to your question
Also, what's the difference between sizeof() and size_t()? and why don't they return the true string lengths? |
in (usually)
/usr/include/sys/types.h:
1 2 3 4 5 6 7
|
typedef ulong_t size_t; /* size of something in bytes */
...
typedef uint_t size_t; /* (historical version) */
...
typedef long ssize_t; /* size of something in bytes or -1 */
...
typedef int ssize_t; /* (historical version) */
| |
sizeof()
I think relies on the original allocated size if you're passing it char arrays as opposed to
strlen()
which looks for the null terminator
Last edited on
Jason2gs, sorry. I'm quite forget about a pthread_create. I listen sample below...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void *squirtIt(char *hName);
char letsGetStarted[128];
int main(int argc, char **argv){
int num_connect;
int ret;
pthread_t tid[35];
sprintf(letsGetStarted, "GET / HTTP/1.0\n");
if (argc != 2){
fprintf(stderr, "Usage: %s <host name> \n", argv[0]);
exit(1);
}
for(num_connect = 0; num_connect < 35; num_connect++){
ret = pthread_create(&tid[num_connect], NULL, (void *)squirtIt,argv[1]);
}
for(num_connect = 0; num_connect < 35; num_connect++){
pthread_join(tid[num_connect], NULL);
}
return 0;
}
| |
Last edited on
Topic archived. No new replies allowed.