Created
April 13, 2014 16:29
-
-
Save paulw54jrn/10591146 to your computer and use it in GitHub Desktop.
TCP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int read_msg_from_socket(int sock_fd,struct sm_msg *msg){ | |
char buffer[sizeof(struct sm_msg)] = {}; | |
size_t buffer_len = 0; | |
int nbytes = 0; | |
if( NULL == msg ){ | |
return -1; | |
} | |
do{ | |
nbytes = recv(sock_fd,buffer + buffer_len,sizeof(struct sm_msg) - buffer_len,MSG_WAITALL); | |
if( nbytes > 0 ){ | |
buffer_len += nbytes; | |
} | |
#ifdef DEBUG | |
printf("Bytes receved -> %d , need more bytes %d \n",nbytes,(int)sizeof(struct sm_msg)-(int)buffer_len); | |
#endif | |
}while( buffer_len < sizeof(struct sm_msg)); | |
sm_cast_mem_to_msg(buffer,sizeof(struct sm_msg),msg); | |
return nbytes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment