 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
SVC1 Guest
|
Posted: Wed Feb 22, 2006 10:03 pm Post subject: TclientSocket |
|
|
Hi,
I am using TclientSocket object to implement a simple
communication protocol, which sends a message and waits for an
answer:
//---------------------------------------------------------------
void __fastcall TForm1::btnExchangeClick(TObject *Sender)
{
ClientSocket = new TClientSocket(Application);
ClientSocket->ClientType = ctNonBlocking;
ClientSocket->OnError = this->ClientSocket1Error;
ClientSocket->OnRead = this->ClientSocket1Read;
// SetUp IP address and port
ClientSocket->Host = 172.2.2.4;
ClientSocket->Port = 950;
_RxString = ""; // Internal AnsiString
ClientSocket->Open();
ClientSocket->Socket->SendText(TxMessage); // send a message
Sleep(1000);
AnsiString Received_Msg = _RxString;
//
//
later ClientSocket is deleted
}
//---------------------------------------------------------------
void __fastcall TForm1::ButtonDisconnectClick(TObject *Sender)
{
ClientSocket->Close();
}
//---------------------------------------------------------------
void __fastcall TEthernet::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
_RxString += Socket->ReceiveText();
}
//---------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent,
int &ErrorCode)
{
//
}
//---------------------------------------------------------------
The problem Im having is even if the program sleeps for a
long period of time, the receive event is occurring after the
function exists (i.e, ClientSocket1Read occurs and I get the
right answer).
In other words, the Sleep is not relinquishing the program for
the ClientSocket1Read() event to occur.
Any hints?
Thanks in advance.
SVC |
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Thu Feb 23, 2006 12:03 am Post subject: Re: TclientSocket |
|
|
SVC1 wrote:
| Quote: | I am using TclientSocket object to implement a simple
communication protocol, which sends a message and waits for an
answer:
|
Well you do not wait. As soon as you did a Sendtext you should leave the onclick
handler and then do the reading in the OnRead event. More like this:
void __fastcall TForm1::btnExchangeClick(TObject *Sender)
{
ClientSocket = new TClientSocket(Application);
ClientSocket->ClientType = ctNonBlocking;
ClientSocket->OnError = ClientSocket1Error; // no this->
ClientSocket->OnRead = ClientSocket1Read; // no this->
// SetUp IP address and port
ClientSocket->Host = 172.2.2.4;
ClientSocket->Port = 950;
ClientSocket->Open();
ClientSocket->Socket->SendText(TxMessage);
}
void __fastcall TEthernet::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
_RxString = ""; // Internal AnsiString
AnsiString Received_Msg = _RxString;
_RxString += Socket->ReceiveText();
//
//
later ClientSocket is deleted
}
Did you plan to create a new TclientSocket for every message ?
Hans. |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Thu Feb 23, 2006 1:03 am Post subject: Re: TclientSocket |
|
|
"SVC1" <svchamlian (AT) hotmail (DOT) com> wrote in message
news:43fcd725$1 (AT) newsgroups (DOT) borland.com...
| Quote: | I am using TclientSocket object to implement a simple communication
protocol |
For future reference, this should have been asked in the .internet.socket
newsgroup instead.
| Quote: | which sends a message and waits for an answer:
|
Your code is not set up properly for that.
| Quote: | ClientSocket->ClientType = ctNonBlocking;
|
Non-blocking sockets rely on window messages being posted internally. You
cannot do your Connect(), Write() and Read() all in the same function,
unless you process pending messages in between. Sleeping alone is not
adequate enough. You should be dividing up your code into even more event
handlers than you are already using, ie:
AnsiString RxString;
AnsiString TxString;
TClientSocket *ClientSocket = NULL;
void __fastcall TForm1::btnExchangeClick(TObject *Sender)
{
if( !ClientSocket )
{
ClientSocket = new TClientSocket(this);
ClientSocket->ClientType = ctNonBlocking;
ClientSocket->OnError = this->ClientSocket1Error;
ClientSocket->OnConnect = this->ClientSocket1Connect;
ClientSocket->OnRead = this->ClientSocket1Read;
ClientSocket->OnWrite = this->ClientSocket1Write;
}
else
ClientSocket->Close();
// SetUp IP address and port
ClientSocket->Host = "172.2.2.4";
ClientSocket->Port = 950;
RxString = "";
TxString = "whatever";
ClientSocket->Open();
}
void __fastcall TForm1::ButtonDisconnectClick(TObject *Sender)
{
if( ClientSocket )
{
delete ClientSocket;
ClientSocket = NULL;
}
}
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
ClientSocket1Write(Sender, Socket);
}
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
_RxString += Socket->ReceiveText();
// use _RxString as needed ...
}
void __fastcall TForm1::ClientSocket1Write(TObject *Sender,
TCustomWinSocket *Socket)
{
int written;
while( Length(TxMessage) > 0 )
{
written = Socket->SendBuf(TxMessage.c_str(), Length(TxMessage));
if( written < 1 )
return;
TxString = TxString.SubString(written+1, MaxInt);
}
}
void __fastcall TForm1::ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
//...
Socket->Close();
ErrorCode = 0;
}
Otherwise, use a blocking socket instead:
| Quote: | The problem I'm having is even if the program "sleeps" for a long
period of time, the receive event is occurring after the function exists
|
As well it should be. Sleeping does not process any messages from the
message queue at all. The events for a non-blocking socket are not
triggered until the relevant messages are received from the queue.
Gambit |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|