c++ builder 里面阻塞模式的收发数据主要理解TWinSocketStream这个流。
TWinSocketStream的官方示例代码如下:
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();
}
}
}
下面为一个比较简单的TClientSocket阻塞用法
void __fastcall TForm1::btn_1Click(TObject *Sender)
{
//1.初始化一些socket参数
ClientSocket1->Address="127.0.0.1";
ClientSocket1->Port=10101;
ClientSocket1->ClientType=ctBlocking;//此处表示socket是阻塞模式
int TIMEOUT=300000,MAXLENGTH=1024;
TWinSocketStream *pClientStream;//TWinSocketStream to read or write information over a blocking socket connection
pClientStream=new TWinSocketStream(ClientSocket1->Socket,TIMEOUT);
//2.连接Server端
ClientSocket1->Open();
//3.发送数据
char sendBuf[1024];
int sendCount=0;
strcpy(sendBuf,E_send->Text.c_str()) ;
sendCount=pClientStream->Write(sendBuf,strlen(sendBuf)+1);
if(sendCount==0) ShowMessage("发送失败!");
//4.接收数据
char receiveBuf[1024];
int Len=0;
if (pClientStream->WaitForData(TIMEOUT))
//在TIMEOUT时间内如果没有接收到数据程序是直会在此挂起,因此如果阻塞模式接收数据的话一般放在线程里面
Len=pClientStream->Read(receiveBuf,MAXLENGTH);
receiveBuf[Len]=0;
Memo1->Lines->Add(receiveBuf);
}
TWinSocketStream的官方示例代码如下:
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();
}
}
}
下面为一个比较简单的TClientSocket阻塞用法
void __fastcall TForm1::btn_1Click(TObject *Sender)
{
//1.初始化一些socket参数
ClientSocket1->Address="127.0.0.1";
ClientSocket1->Port=10101;
ClientSocket1->ClientType=ctBlocking;//此处表示socket是阻塞模式
int TIMEOUT=300000,MAXLENGTH=1024;
TWinSocketStream *pClientStream;//TWinSocketStream to read or write information over a blocking socket connection
pClientStream=new TWinSocketStream(ClientSocket1->Socket,TIMEOUT);
//2.连接Server端
ClientSocket1->Open();
//3.发送数据
char sendBuf[1024];
int sendCount=0;
strcpy(sendBuf,E_send->Text.c_str()) ;
sendCount=pClientStream->Write(sendBuf,strlen(sendBuf)+1);
if(sendCount==0) ShowMessage("发送失败!");
//4.接收数据
char receiveBuf[1024];
int Len=0;
if (pClientStream->WaitForData(TIMEOUT))
//在TIMEOUT时间内如果没有接收到数据程序是直会在此挂起,因此如果阻塞模式接收数据的话一般放在线程里面
Len=pClientStream->Read(receiveBuf,MAXLENGTH);
receiveBuf[Len]=0;
Memo1->Lines->Add(receiveBuf);
}