- 论坛徽章:
- 0
|
回复 11# haoxifer
贴上代码,大家帮忙看看代码是否存在问题呢?谢谢各位了~!- int socket_send(int socket_fd, const char *send_data, int send_len){
- int once_write,offset;
-
- if(socket_fd < 0)
- {
- printf("Invalid socket[%d]! -- [%s][socket_send][%d]\n",socket_fd,__FILE__,__LINE__);
- sprintf(comm_err_msg,"Invalid socket[%d]! -- [%s][socket_send][%d]\n",socket_fd,__FILE__,__LINE__);
- return FAILED;
- }
-
- if(send_len <= 0)
- {
- printf("Invalid send length[%d]! -- [%s][socket_send][%d]\n",send_len,__FILE__,__LINE__);
- sprintf(comm_err_msg,"Invalid send length[%d]! -- [%s][socket_send][%d]\n",send_len,__FILE__,__LINE__);
- return FAILED;
- }
- #ifdef DEBUG
- printf("Send data [%s], Send len [%d]\n", send_data, send_len);
- #endif
-
- /*对于流式套接字,write方法返回实际发送得字节数,可能会小于待发送得总字节数*/
- offset=0;
- while(offset < send_len)
- {
- once_write=write(socket_fd, send_data + offset, send_len - offset);
- if(once_write <= 0)
- {
- printf("socket write error! has write length[%d]! -- %s[%s][socket_send][%d]\n",offset,strerror(errno),__FILE__,__LINE__);
- sprintf(comm_err_msg,"socket write error! has write length[%d]! -- %s[%s][socket_send][%d]\n",offset,strerror(errno),__FILE__,__LINE__);
- return FAILED;
- }
- offset += once_write;
- }
-
- return send_len;
- }
复制代码 |
|