请看以下代码:#define MAX_BUFFER_SIZE 4096
// Initialize critical section (data) and create
// an auto-reset event (hEvent) before creating the
// two threads
CRITICAL_SECTION data;
HANDLE hEvent;SOCKET sock;
TCHAR buff[MAX_BUFFER_SIZE];
int done=0;
// Create and connect sock
...
// Reader thread
void ReadThread(void)
{
int nTotal = 0,
nRead = 0,
nLeft = 0,
nBytes = 0; while (!done)
{
nTotal = 0;
nLeft = NUM_BYTES_REQUIRED;
// However many bytes constitutes
// enough data for processing
// (i.e. non-zero)
while (nTotal != NUM_BYTES_REQUIRED)
{
EnterCriticalSection(&data);
nRead = recv(sock, &(buff[MAX_BUFFER_SIZE - nBytes]),
nLeft, 0);
if (nRead == -1)
{
printf("error\n");
ExitThread();
}
nTotal += nRead;
nLeft -= nRead; nBytes += nRead;
LeaveCriticalSection(&data);
}
SetEvent(hEvent);
}
}// Computation thread
void ProcessThread(void)
{
WaitForSingleObject(hEvent); EnterCriticalSection(&data);
DoSomeComputationOnData(buff);
// Remove the processed data from the input
// buffer, and shift the remaining data to
// the start of the array
nBytes -= NUM_BYTES_REQUIRED; LeaveCriticalSection(&data);
}
问题:
nRead = recv(sock, &(buff[MAX_BUFFER_SIZE - nBytes])填入缓冲区为什么不是从buff[nBytes]开始填入?上面的两个函数分别对应两个线程,一个是读取数据到REQUIRE的程度就通知另一个线程,而另一个是处理线程。
// Initialize critical section (data) and create
// an auto-reset event (hEvent) before creating the
// two threads
CRITICAL_SECTION data;
HANDLE hEvent;SOCKET sock;
TCHAR buff[MAX_BUFFER_SIZE];
int done=0;
// Create and connect sock
...
// Reader thread
void ReadThread(void)
{
int nTotal = 0,
nRead = 0,
nLeft = 0,
nBytes = 0; while (!done)
{
nTotal = 0;
nLeft = NUM_BYTES_REQUIRED;
// However many bytes constitutes
// enough data for processing
// (i.e. non-zero)
while (nTotal != NUM_BYTES_REQUIRED)
{
EnterCriticalSection(&data);
nRead = recv(sock, &(buff[MAX_BUFFER_SIZE - nBytes]),
nLeft, 0);
if (nRead == -1)
{
printf("error\n");
ExitThread();
}
nTotal += nRead;
nLeft -= nRead; nBytes += nRead;
LeaveCriticalSection(&data);
}
SetEvent(hEvent);
}
}// Computation thread
void ProcessThread(void)
{
WaitForSingleObject(hEvent); EnterCriticalSection(&data);
DoSomeComputationOnData(buff);
// Remove the processed data from the input
// buffer, and shift the remaining data to
// the start of the array
nBytes -= NUM_BYTES_REQUIRED; LeaveCriticalSection(&data);
}
问题:
nRead = recv(sock, &(buff[MAX_BUFFER_SIZE - nBytes])填入缓冲区为什么不是从buff[nBytes]开始填入?上面的两个函数分别对应两个线程,一个是读取数据到REQUIRE的程度就通知另一个线程,而另一个是处理线程。
两个线程公用一个 buf,如果都填都不,那么处理线程还没处理完的时候数据就被读线程把数据覆盖了。
nRead = recv(sock, buff + nBytes, nLeft, 0);
试试,nRead = recv(sock, &(buff[MAX_BUFFER_SIZE - nBytes]),
nLeft, 0);
应该会越界。
nBytes += nRead;
你仔细琢磨一下就明白了 nRead 是实际读的字节数 仔细看代码就明白了
应该是错误的,很多书上有错误的代码。
应该是&buff[nBytes]看书要有辨别能力
1.楼主说的 recv(sock, &(buff[MAX_BUFFER_SIZE - nBytes]),最开始的时候nbytes是0,那不是从buf最后面开始接收吗,这样不明显报错吗?
2.while (nTotal != NUM_BYTES_REQUIRED) 判断是否达到请求的数量。这个怎么确定呢?如何准确的确定有多少个要接收的?如果不能确定,不是一直循环在这了吗?
3。当接收完要请求的数量后,while退出,也就是nTotal == NUM_BYTES_REQUIRE,SetEvent,处理线程开始工作。nBytes -= NUM_BYTES_REQUIRED;这样能使接收线程开始工作吗,也就是符合.while (nTotal != NUM_BYTES_REQUIRED)?如果这个条件不成立的时候,接收线程,不是一直在SetEvnet这个吗,不变成独占CPU了?