Lost recieved data when calling recv twice in C socket -


i'm trying read first 4 bytes received network whole message length message this:

#include <sys/socket.h> int len,length; char *buffer, lengthx[4]; recv(com_handle, lengthx, 4, 0); sscanf(lengthx, "%" s(4) "x", &len); length=recv(com_handle, buffer , len, 0); 

for reasons loose 13 bytes of rest of message. ideas?

the recv() function not guarantee read full number of bytes specified. linux manual page describes way:

the receive calls return data available, requested amount, rather waiting receipt of full amount requested.

in way read(2), prefer purposes. type of socket (stream vs. datagram) impacts this, either function, may need prepared loop, reading repeatedly in order collect pieces of message.

and always check return value of syscalls. in case, not need detect errors, need ensure read correctly @ all.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -