Update:
I add a while to get the remain data, problem solved. thanks you guys.
while(res != rcvread->size + 4) {/* do not get full data */
tmp = recv(connfd, (void *)rcvread + res, rcvread->size + 4 - res, 0);
if(tmp == -1)
printf("error:%s\n",(char *)strerror(errno));
res+=tmp;
}
I want to send a structure mfsio through socket, the return value of write is 37772, but when I got it from the other program, the return value of read is 32768, that's really odd.
The definition of this structure is here
struct mfsio{
size_t size;
char buf[];
};
The send code is here
struct mfsio *sndread;
sndread = malloc(sizeof(struct mfsio) + rcvcmd->size);
res = pread(fd, &(sndread->buf), rcvcmd->size, rcvcmd->offset);
sndread->size = res;
res = write(connfd, sndread, sizeof(struct mfsio) + res);
The receive code is here
struct mfsio *rcvread;
rcvread = malloc(sizeof(struct mfsio) + size);
res = 0;
res = read(connfd, rcvread, sizeof(struct mfsio) + size);
size equals to rcvcmd->size, and the res of pread is 32678, the res of write is 32772.
but res of read is 32678.
How can such thing happened? Does I do something wrong here?
If there is no input from write, the read function will just waiting for some data and just hanging there
If I use a loop to avoid such problem, how can I finally get the entire data structure, I mean if I do loop reading, I will get the remain bytes from the socket, but how can I combine these clips I get?