|
|
От: |
VVV
|
|
| Дата: | 20.02.03 15:53 | ||
| Оценка: | |||
long
TSockHelper::Recv(SOCKET sock, void *Buf_, int iSize, int iTimeout)
{
TIMEVAL TheTime={1, 0};
int err;
int rc;
fd_set readfds;
fd_set exceptfds;
FD_ZERO(&readfds);
FD_ZERO(&exceptfds);
int iShift=0;
char *Buf=(char*)Buf_;
while(iSize > 0)
{
FD_SET(sock, &readfds);
FD_SET(sock, &exceptfds);
err=::select(0, &readfds, 0, &exceptfds, &TheTime);
if(err == SOCKET_ERROR || FD_ISSET(sock, &exceptfds))
return SOCKET_ERROR;
if(FD_ISSET(sock, &readfds))
{
rc=::recv(sock, Buf+iShift, iSize, 0);
if(rc == SOCKET_ERROR)
return SOCKET_ERROR;
iShift+=rc;
iSize-=rc;
}
else
if(err == 0)
{
if(iTimeout < 0 && --iTimeout == 0)
return SOCKET_ERROR;
}
}
return iShift;
}
//вызов
struct MyData{
...
};
MyData data;
TSockHelper::Recv(sock, &data, sizeof(data), 10); //10 seconds timeout realloc(buff,sizeof(PckHd)+((PckHd*)buff)->CurrPacketSize);
recv(s,buff,sizeof(buff),0);поэтому верное использование такое:The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument.
buff=realloc(buff,sizeof(PckHd)+((PckHd*)buff)->CurrPacketSize); int rc=recv(s, buff+sizeof(PckHd), ((PckHd*)buff)->CurrPacketSize,0);