Indy10 > WriteLn() 调用后如何等待
Indy10 > How to wait after WriteLn() call
我的环境:
Windows 7 Pro (32bit)
C++ Builder XE4
我想了解 WriteLn() 之后的等待;
以下是我的示例代码。
void __fastcall TForm1::IdTCPServer1Execute(TIdContext *AContext)
{
UTF8String rcvdStr;
rcvdStr = AContext->Connection->IOHandler->ReadLn(
IndyTextEncoding(TEncoding::UTF8) );
TList *threads;
TIdContext *ac;
threads = IdTCPServer1->Contexts->LockList();
ac = reinterpret_cast<TIdContext *>(threads->Items[0]);
UTF8String sendStr;
sendStr = "send:" + rcvdStr;
ac->Connection->IOHandler->WriteLn(sendStr);
for(int idx=0; idx<10; idx++) {
Sleep(100);
Application->ProcessMessages();
}
ac->Connection->Disconnect();
IdTCPServer1->Contexts->UnlockList();
}
//---------------------------------------------------------------------------
我将 wait (for(int idx=0;...) 放在 WriteLn() 之后,以便在断开连接之前完成发送。但是,我不确定这是否是正确的等待方式。我也不知道我应该等多久(在这个例子中,我等了 1000 毫秒)。
问:有没有什么函数可以知道WriteLn()完成的?
您根本不需要等待。 WriteLn()
是一个阻塞函数。在将整个字符串放入套接字的出站缓冲区之前,它不会退出。默认情况下,启用套接字的 LINGER 选项,这意味着关闭的套接字将在完全关闭端口之前尝试在后台发送挂起的出站数据,即使在您的代码继续进行之后也是如此。
参考 MSDN 了解更多详情:
Graceful Shutdown, Linger Options, and Socket Closure
郑重声明,Indy 的 Disconnect()
确实同时使用了 shutdown()
和 closesocket()
。
我的环境:
Windows 7 Pro (32bit)
C++ Builder XE4
我想了解 WriteLn() 之后的等待; 以下是我的示例代码。
void __fastcall TForm1::IdTCPServer1Execute(TIdContext *AContext)
{
UTF8String rcvdStr;
rcvdStr = AContext->Connection->IOHandler->ReadLn(
IndyTextEncoding(TEncoding::UTF8) );
TList *threads;
TIdContext *ac;
threads = IdTCPServer1->Contexts->LockList();
ac = reinterpret_cast<TIdContext *>(threads->Items[0]);
UTF8String sendStr;
sendStr = "send:" + rcvdStr;
ac->Connection->IOHandler->WriteLn(sendStr);
for(int idx=0; idx<10; idx++) {
Sleep(100);
Application->ProcessMessages();
}
ac->Connection->Disconnect();
IdTCPServer1->Contexts->UnlockList();
}
//---------------------------------------------------------------------------
我将 wait (for(int idx=0;...) 放在 WriteLn() 之后,以便在断开连接之前完成发送。但是,我不确定这是否是正确的等待方式。我也不知道我应该等多久(在这个例子中,我等了 1000 毫秒)。
问:有没有什么函数可以知道WriteLn()完成的?
您根本不需要等待。 WriteLn()
是一个阻塞函数。在将整个字符串放入套接字的出站缓冲区之前,它不会退出。默认情况下,启用套接字的 LINGER 选项,这意味着关闭的套接字将在完全关闭端口之前尝试在后台发送挂起的出站数据,即使在您的代码继续进行之后也是如此。
参考 MSDN 了解更多详情:
Graceful Shutdown, Linger Options, and Socket Closure
郑重声明,Indy 的 Disconnect()
确实同时使用了 shutdown()
和 closesocket()
。