如何使用TWinSocketStream的WaitForData()函数
CB的帮助文件里有一段关于网络编程的代码:
void __fastcall TMyServerThread::ClientExecute(void)
{
TWinSocketStream *pStream;
char Buffer[10];
// make sure connection is active
while (!Terminated && ClientSocket->Connected)
{
try
{
Stream = new TWinSocketStream(ClientSocket, 60000);
try
{
memset(Buffer, 0, 10); // initialize the buffer
// give the client 60 seconds to start writing
if (pStream->WaitForData(60000))
{
if pStream->Read(Buffer, 10) == 0) // if can抰 read in 60 seconds
ClientSocket->Close(); // close the connection
// now process the request
...
}
else
ClientSocket->Close(); // if client doesn抰 start, close
}
__finally
{
delete pStream;
}
}
catch (Exception &E)
{
HandleException();
}
}
}
我想就这段代码请教大家几个问题:
1、WaitForData()的功能是什么?如果返回true是表示已经与服务器建立连接还是表示 服务器端已经有数据要发送?如果返回为false是否表明已经断开了与服务器端的连接。
2.如果ClientSocket是非阻塞方式是否还有必要调用WaitForData()函数。
3.假设服务器端意外断开连接,本程序可以通过那个语句判断出来。
4.每次调用read和write函数,其发送/接收的字节数是否有限制,与什么相关。
问题点数:0、回复次数:0Top



