Skip to content

Instantly share code, notes, and snippets.

@paulw54jrn
Created April 13, 2014 16:29
Show Gist options
  • Save paulw54jrn/10591146 to your computer and use it in GitHub Desktop.
Save paulw54jrn/10591146 to your computer and use it in GitHub Desktop.
TCP
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