请问int a=send(socket,buf,len,flags) 这个a是代表发送的包的大小吗?(各个参数值已知的情况下)能不能在发送之前就获得这个a的值?

wellin 2003-09-26 02:16:31
请问int a=send(socket,buf,len,flags) 这个a是代表发送的包的大小吗?(各个参数值已知的情况下)能不能在发送之前就获得这个a的值?
...全文
195 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovemaggic 2003-09-29
  • 打赏
  • 举报
回复
a是发送成功的字节数,函数调用返回的结果,如楼上朋友所说,可以
设置循环发送,直到全部数据发送完毕.在MFC中可以重载CAsyncSOcket::OnSend()
发送数据.
broadken 2003-09-29
  • 打赏
  • 举报
回复
同志们真的是这样啊?
不明白中。。。。。
有没有高过测试啊。
如果发送10个字节。一次发送了五个,返回5,另一次发送5个。是这样吗?
我做个实验把

cnpr 2003-09-28
  • 打赏
  • 举报
回复
http://www.eaoo.com/design/list.asp?classid=2&Nclassid=13
Darkay_Lee 2003-09-28
  • 打赏
  • 举报
回复
贴上来才知道有个注释错了
sendAll的注释应该是这样

// Return Value : If no error occurs, recv returns the number of bytes send.
Darkay_Lee 2003-09-28
  • 打赏
  • 举报
回复
danfeng(丹枫)
建议看一下《高级TCP/IP编程》你就真正理解“tcp 本来就是保证数据的安全性的”的含义了

pengweibo(马甸尼)
我好讨厌匈牙利命名法啊!(纯粹个人意见,莫见怪.^_^。)
//////////////////////////////////////////////////////////////////////////////
// Function Name : sendAll
// Description : try to send all the outbuf
// Input Parameters : outbuf -- the data which want to be send
// want -- how many bytes you wan to send
// Return Value : If no error occurs, recv returns the number of bytes received.
// If the connection has been gracefully closed, the return value is zero.
/// Otherwise, a value of SOCKET_ERROR is returned
//////////////////////////////////////////////////////////////////////////////
int AbstractSocket::sendAll(const char *outbuf, int want) const
{
assert(m_socket!=INVALID_SOCKET);
assert(outbuf);
assert(want>0);

int left;
int idx;
int ret;
left = want;
idx = 0;
while(left > 0)
{
ret = ::send(m_socket, &outbuf[idx], left, 0);
// send error, or has been close by peer
if(ret == 0 || ret == SOCKET_ERROR)
break;
left -= ret;
idx += ret;
}
// left == 0 means we do the job successfully
return left == 0 ? idx : ret;
}

//////////////////////////////////////////////////////////////////////////////
// Function Name : recvAll
// Description : try to recv all data we want
// Input Parameters : inbuf -- where to store data
// want -- how much data we want
// Return Value : If no error occurs, recv returns the number of bytes received.
// If the connection has been gracefully closed, the return value is zero.
/// Otherwise, a value of SOCKET_ERROR is returned
//////////////////////////////////////////////////////////////////////////////
int AbstractSocket::recvAll(char *inbuf, int want) const
{
assert(m_socket!=INVALID_SOCKET);
assert(inbuf);
assert(want>0);

int ret;
int read = 0;
while(read < want)
{
ret = recv(&inbuf[read], want-read);
// send error, or has been close by peer
if(ret == 0 || ret == SOCKET_ERROR)
break;
read += ret;
}
// read == want means we do the job successfully
return read == want ? read : ret;
}
danfeng 2003-09-28
  • 打赏
  • 举报
回复
那你还不如就用阻塞模式
tcp 本来就是保证数据的安全性的
pengweibo 2003-09-28
  • 打赏
  • 举报
回复
楼上,完全同意。
如果要保证数据完全发送或接送,最好自己重写send 和 recv.


int CTcpSocket::Recvn(char *lpstrBuf,int nCount,int nFlags)
{
int nRecv, nLeft;
char *ptr;

ptr = lpstrBuf;
nLeft = nCount;

while ( nLeft > 0 )
{
nRecv = recv(m_nSock,ptr,nLeft,nFlags);
if ( nRecv<=0 )
{
SetError(READ_ERROR);
if ( nRecv==0 ) // the server socket is closed.
{
return -1;
}
else
{
if ( GetError()==WSAETIMEDOUT )
{
break;
}
else
{
return -1;
}
}
}

nLeft -= nRecv;
ptr += nRecv;
}

return (nCount-nLeft);
}
/////////////////////////////////////////////////////////////////////////////

int CTcpSocket::Sendn(const char *lpstrBuf,int nCount,int nFlags)
{
int nSend, nLeft;
const char *ptr;

ptr = lpstrBuf;
nLeft = nCount;

while ( nLeft > 0 )
{
nSend = send(m_nSock,ptr,nLeft,nFlags);
if ( nSend<=0 )
{
SetError(WRITE_ERROR);
if ( GetError()==WSAETIMEDOUT )
{
nSend = 0;
}
else
{
return -1;
}
}

nLeft -= nSend;
ptr += nSend;
}

return (nCount-nLeft);
}
/////////////////////////////////////////////////////////////////////////////
lazycat818 2003-09-26
  • 打赏
  • 举报
回复
不能。a表示本次Send实际上发送了多少字节。注意,Send不能保证把你要发送的数据全部发送。如你发10240字节,它可能只发走10000字节,如果使用的是阻塞的Socket,它会接着发送,直到发送完毕(或超时,或发生错误)。如果是非阻塞的,那么它不会自己接着发送。

具体可以参考一下CSocket和CAsyncSOcket的MFC源代码。

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧