I am sending data (10 files, approx 10-20 kb each) using sockets in C++ on Linux.
My pseudo code in the server:
for(i=0;i<10;i++){ ret = send(sockfd, filedata, filedatasize, 0); } ret returns the correct file size for all data files, no errors, but the client only receives 4-5 files.
But, if I use this instead:
for(i=0;i<10;i++){ ret = send(sockfd, filedata, filedatasize, 0); sleep(1); } The client receives all files, and not errors!
What is the problem?
I tried tweaking SO_RCVBUF and SO_SNDBUF, but no changes.
EDIT: (the complete code has 200000 lines to paste!)
Here is the server code:
int Socket::sendMsg(char * data, unsigned int uiSize) { unsigned short * us; int i, iRet; us = (unsigned short *)(m_pSendBuffer); *us = (unsigned short)(uiSize + 2); memcpy((char *)m_pSendBuffer + 2, data, uiSize); iRet = isend(m_pSendBuffer, uiSize + 2); if (iRet<0) { return SOCKET_ERROR; } return iRet; } int Socket::isend(char * data, int size) { int outlen, ret; outlen= 0; while (outlen< size) { ret= send(sockfd, (data+ outlen), size - outlen, 0); if (ret< 0) { return -1; } else outlen+= ret; } return outlen; } Here is the client code:
void XSocket::onRead() { int iRet; unsigned short * usp; unsigned int uiReadSize; while (1) { iRet = recv(m_Sock, (char *)m_pRecvBuffer, 2, 0); uiReadSize = 0; usp = (unsigned short *)(m_pRecvBuffer); uiReadSize = (int)(*usp - 2); iRet = recv(m_Sock, (char *)(m_pRecvBuffer + 2), uiReadSize, 0); if (iRet < 0) { //return error } else if (iRet == 0) { //disconnect } //PROCCESS MSG } }