Originally Posted by
vart
your ptr pointer points into some address that has nothing to do with storage allocated with malloc/realloc... so you writing to some random address...
I was surprised by this but you're right. Anyway, I changed it to use a buffer with a fixed size:
Code:
int Recept (int sock, char *buffer, int size) {
int count=0;
char *ptr=buffer;
while (recv(sock,ptr,1,0)>0) {
if (ptr[0]=='\n') {
if (buffer[count-1]=='\r') buffer[count-1]='\0';
else buffer[count]='\0';
return count;}
ptr++; count++;
if (count>=size) return -2;
}
perror("Recept() -- recv");
return -1;
}
and the strange thing that happens now is this: I use it looped in one function for the reply to a HEAD request. No problems. Then I send GET on the same item (a binary) so I want to skip thru the head:
Code:
for (i=o;i<10;i++) {
tmp=Recept(sock,buffer,4096); // exactly the same line as in the other function
printf("%d %s\n",tmp,buffer); // just to check
}
At every iteration including the first one, I get:
Recept() -- recv: Illegal seek
-1
I've googled around for why recv would return this error and no one seems to have sorted it out, but I still have my fingers crossed
It would seem that the recv here doesn't work at all, because the next thing is I want the binary and I have Content-Length, so I just do recv(sock,buffer,Content-Length,0), which does work except it includes the ten lines of the head I wanted to skip -- ie, Recept did nothing but fail.