send()如何保证已发送完毕?
我想在客户端退出的时候给服务器发消息,准备调用两次send(),然后调用WSACleanup(),但是据我测试,服务器还没收到就已经触发了客户端的WSACleanup()事件。
看:
void CChatDlg::OnExitRoom(){ send(g_ClientSocket, (char*)&head, sizeof(head), 0); send(g_ClientSocket, theApp.m_strUserName.GetBuffer(0), head.m_StrLen, 0); WSACleanup();}
int CChatDlg::mySend(SOCKET socket, const void* lpBuf, int nBufLen, int nFlags){ char* pBuf = (char*)lpBuf; int nLeft = nBufLen; int nWritten = 0; while (nLeft > 0) { nWritten = send(socket, pBuf, nLeft, nFlags); if (nWritten == SOCKET_ERROR) { return nWritten; } nLeft -= nWritten; pBuf += nWritten; } return nBufLen - nLeft;}