hi there,
sorry, i don't know where is the suitable subforum for this question, admin please move it to where it fits.
i am learning, writing a iocp winsock server using c language.(based on some source code online).
i don't quite understand the following
Code: |
|
while(1) {
if( GetQueuedCompletionStatus(cport, &btrans, (LPDWORD) &phandledata, (LPOVERLAPPED *)&piodata, INFINITE) == 0 ) {
LogTime();
printf("GetQueuedCompletionStatus() failed - %d\n", GetLastError());
return 0;
}
if( btrans > 0) {
printf("btrans > 0 : %d bytes\n", btrans);
//printf("%s\n", piodata->buffer);
ZeroMemory( &(piodata->ov), sizeof(OVERLAPPED) );
//ZeroMemory( &(piodata->buffer), sizeof(BUFFERSIZE) );
piodata->wsabuff.len = BUFFERSIZE;
piodata->wsabuff.buf = piodata->buffer;
flags = 0;
if(WSARecv( phandledata->s, &(piodata->wsabuff), 1, &brecv, &flags, &(piodata->ov), NULL ) == SOCKET_ERROR) {
if( WSAGetLastError() != ERROR_IO_PENDING ) {
LogTime();
printf("WSARevc() 182 failed %d\n", WSAGetLastError());
return 1;
}
}
}
if( brecv > 0) {
printf("brecv > 0 : %d bytes\n", brecv);
} else {
printf("brecv <= 0\n");
}
if( btrans == 0 ) {
printf("btrans ==0\n");
}
}
|
|
above is the worker thread partial code.
am using the quetannon client to access my server.
btrans == 0 means disconnect,
btrans > 0 means i receive something
if the buffer is large enough to receive incoming data 1 time, then brecv will be <= 0, if buffer is not large enough, then brecv will equal buffersize (full load)
then it will repeat btrans > 0 part and do WSARecv
the output i got is like this
Code: |
|
2011-12-31 18:46:35 (296) 1912 -accepted
btrans > 0 : 40 bytes
brecv <= 0
btrans > 0 : 1024 bytes
brecv > 0 : 1024 bytes
btrans > 0 : 1024 bytes
brecv > 0 : 1024 bytes
btrans > 0 : 1024 bytes
brecv > 0 : 156 bytes
btrans > 0 : 156 bytes
brecv > 0 : 156 bytes
brecv > 0 : 156 bytes
btrans ==0
^C
|
|
what i don't understand is,
1. should i receive full incoming data, then only i start doing something, eg. response back.
2. how should i check/peek the WSARecv data length? i want to close socket on any socket that send out > 512bytes to me.
3. could send & recv occur concurrently? or after recv finish, then only send? and what would happen if during send i got recv
4. how to terminate browser request? send back http header with close connection? or closesocket?
thank you very much...