nSendNum = sendto(pbuf, 2048);
if (nSendNum==SocketError)
{除了WSAEWOULDBLOCK  }
如果 发送成功 nSendNum会小于2048吗,
如果小于,我该怎么处理剩下的呢,想tcp流式的一样吗(在Onsend中处理) 

解决方案 »

  1.   

    发送成功 nSendNum会小于2048吗,
    ==要看你pBuf中到底有多少内容啊。如果不是满的,自然会小于2048了。
      

  2.   

    但,msdn 说:
    Return Values
    If no error occurs, this function returns the total number of bytes sent, which can be less than the number indicated by len. 
      

  3.   

    //发送消息到目的地址
    int DoSendTo(int sock, char* msg, int len, struct sockaddr_in sa){ int tolen=0;                        
    if(sock<=0) return -1; PostMessage(AfxGetMainWnd()->m_hWnd, SHOW_LOG,0, (LPARAM)&msg[16]); tolen=sendto(sock,msg,len,0,(struct sockaddr *)&sa,sizeof(struct sockaddr));
    return tolen;
    }
    应该没有问题的呀
      

  4.   

    但,msdn 说:
    Return Values
    If no error occurs, this function returns the total number of bytes sent, which can be less than the number indicated by len. 
    ==说的很对啊。虽然你指定长度为2048,但pBuf中不一定有那么长的内容啊。
      

  5.   

    我的意思,你发2048,就返回2048吗, 当然buf 的长度肯定大于2048
      

  6.   

    TO:happyparrot(快乐鹦鹉)
    如果你的发送缓冲区大于2048的话,只要发送成功,应该不用再发了。
    --------------
    ==说的很对啊。虽然你指定长度为2048,但pBuf中不一定有那么长的内容啊。
    ---------------
    首先buf只是个指针,长度一般都没有问题。其次,在buf不存在你说的问题下,一定就返回2048吗(除了异常错误)
      

  7.   

    应该是不一定。如果你是buf比较大,需要分块发送,那么,下一步发送不能直接加2048,而是加上次sendto的返回值。
      

  8.   

    To:happyparrot(快乐鹦鹉) 
    可以给我一个用udp 异步发送的例子吗?
      

  9.   

    while(1)
    {
    memset(buf,0,BUFSIZE);
    //以指定的缓冲区大小分块发送
    nRead = pFile->Read(buf,BUFSIZE);
    theApp.GetActiveView()->m_StartTime = COleDateTime::GetCurrentTime();
    theApp.GetActiveView()->m_nRetryTime = 0;
    //已经读到文件尾,发送结束
    if(nRead == 0)
    break;
    //通过数据通道发送数据
    nSend = m_pDataSocket->Send(buf,nRead,0);
    //如果发送发生错误,返回FALSE
    if(nSend == SOCKET_ERROR)
    {
    theApp.GetActiveView()->SendMessage(WM_PINGREPLY,1,0);
    sInfo.Format("%s发送文件 %s 失败\r\n",UPLOAD_HEAD,pFile->GetFileName());
    NotifyView(WM_TRANSINFOMSG,0,(LPARAM)(&sInfo));
    //写数据库。修改TransFile表中该文件的发送状态信息
    //UPDATE TransFile.StatusID = '101'(发送失败)  原状态为'102'(等待发送)
    m_pDBImp->UpdateTransFileStatus(pFile->GetFileName(),"101"); pFile->Close();
    delete pFile;
    pFile = NULL;
    return FALSE;
    }
    // 如果发送出去的字节少于从文件读取的字节,则调整发送指针,以使得数据发送正确
    if(nRead != nSend) 
    pFile->Seek(nSend-nRead,CFile::current);
    };
    这是我们发送文件时的程序。
      

  10.   

    继续调用sendto(ptr + bytesend, allsize - bytesend)
      

  11.   


    while ( 1 )
    {
      nSendNum = sendto(pbuf, 2048);
      if (nSendNum==SocketError)
         break;      // 发送错误停止
      else if ( nSendNum < 2048 )
         break;      // 最后一次发送完成,所以退出
      else
         pbuf += nSendNum;   // 移动发送缓冲区的指针用于下次发送
    }
      

  12.   

    这样相当于你的packet就被分成几份了,
    你接收的时候就不知道了。
    这个不是跟TCP流式一样
      

  13.   

    实际的程序我当然会考虑MTU的问题, 但现在想知道udp sendto的原理。
    我昨天做了试验,
    发送655XXXX 约是 0xFFFF-一个数, 他发送都会return 这个数字,不会有小这个值的现象