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; | |
} |
![]() |
1
alexapollo 2014-04-14 00:49:13 +08:00
man recv
这是一个基础问题。 |
![]() |
2
xdeng 2014-04-14 01:11:03 +08:00 via iPhone
-1断开?
|
![]() |
3
paulw54jrn OP @alexapollo
man page 我看过,我也明白这是一个基础问题. 也许正是太基础了,网上的描述都比较简单. 按照网上的建议,试了用几种不同的方式来把数据拼接在一起,但是都会有问题. 我觉得是我对Signal和Select的理解不够,所以想看看大家的意见. 除了吐槽之外,还请'请尽量让自己的回复能够对别人有帮助'. |
![]() |
4
vietor 2014-04-14 09:39:18 +08:00
SIGPIPE应该disable掉,同时对于粘包使用一定的拼接缓冲区是无法避免的。此外,使用SIGIO来作为事件通知也太古老了,难道是mac环境?
|