Article View: borland.public.cppbuilder.internet.socket
Article #5360Re: TClientSocket in blocking mode ...
From: Andreas Tirok
Date: Fri, 20 Aug 2004 11:07
Date: Fri, 20 Aug 2004 11:07
274 lines
8271 bytes
8271 bytes
Too be a more clear,
I wrote an app which connects to a server and send a request. It should wait
for an answer.
This should loop until connection is broken. I have the problem, that the
answers of the server won't
go through my receive routine. And on the server site I get an 997-Error
which means I/O pending...
Here is my current client source:
void __fastcall TMainWindow::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
if(MainWindow->ClientSocket1->Active && InitFlag)
{
MainWindow->Display_TcpClient("Connected", D_INF);
TCP_Online=true;
TCPAckFlag=true;
ThreadTcpConnect->Interval = 3000;
ThreadTcpConnect->Suspend(); // Connect Thread
anhalten!!!
m_pSecomClientReadThread = new SecomClientReadThread(ClientSocket1);
SetEvent(g_EvSetup);
ThreadTcpWatchdog->Interval=WatchdogTime; // Watchdog Timer an!!!
ThreadTcpWatchdog->Resume(); // Watchdog Thread
ausf�hren!!!
}
}
//--------------------------------------------------------------------------
-
void __fastcall TMainWindow::ClientSocket1Disconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
NewInitThread->Suspend();
SelftestThread->Suspend();
MonitoringThread->Suspend();
AlarmThread->Suspend();
if(!ThreadTcpWatchdog->Suspended)
ThreadTcpWatchdog->Suspend(); // Watchdog Thread
anhalten!!!
if (NULL != m_pSecomClientReadThread)
{
delete m_pSecomClientReadThread;
m_pSecomClientReadThread = NULL;
}
MainWindow->Display_TcpClient("disconnected", D_INF);
TCP_Online=false;
if(ThreadTcpConnect->Suspended && Init->netInfo.TCPClient)
ThreadTcpConnect->Resume(); // Connect Thread
ausf�hren!!!
}
//--------------------------------------------------------------------------
-
void __fastcall TMainWindow::ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
// TcpClient->TCP_OnError(0, Sender, Socket, ErrorEvent, ErrorCode);
AnsiString l_ErrorText;
AnsiString l_Msg;
switch (ErrorEvent)
{
case eeGeneral: l_ErrorText = "General"; break;
case eeSend: l_ErrorText = "Send"; break;
case eeReceive: l_ErrorText = "Receive"; break;
case eeConnect: l_ErrorText = "Connect"; break;
case eeDisconnect: l_ErrorText = "Disconnect"; break;
case eeAccept: l_ErrorText = "Accept"; break;
default: l_ErrorText = "Unknown"; break;
}
l_Msg.printf("Error: WinSocketAPI [%s], Code=%d, Hd=%d, SockHd=%d",
l_ErrorText.c_str(), ErrorCode, (int) ClientSocket1->Socket->Handle,
(int) ClientSocket1->Socket->SocketHandle);
Display_TcpClient(l_Msg.c_str(), D_INF);
MessageBeep(0);
ThreadTcpConnect->Interval = 60000;
ErrorCode=0;
ClientSocket1->Socket->Close();
}
#####
void __fastcall SecomClientReadThread::Execute()
{
//---- Hier den Thread-Code plazieren----
AnsiString l_Msg;
static bool s_Exception_printed = 0;
static bool s_Exception_printed1 = 0;
try
{
// Befehle abrufen und verarbeiten, bis die Verbindung oder der Thread
beendet ist
m_MyIPAddress = m_ClientSocket->Socket->LocalAddress;
m_MyPort = m_ClientSocket->Socket->LocalPort;
m_pStream = new TWinSocketStream(m_ClientSocket->Socket, 60000);
InitBuffer();
m_InSidePacket = false;
while (!Terminated && m_ClientSocket->Active)
{
int l_Bytes = 0;
try
{
if (WaitForSingleObject(g_EvSetup, 100) == WAIT_OBJECT_0)
CmdSetup();
if(!m_pStream->WaitForData(m_pStream->TimeOut))
continue;
if (Terminated || NULL == m_ClientSocket || !m_ClientSocket->Active) //
disconnect during wait ...
break;
if (m_pStream->Size > 0)
{
l_Msg.printf("%s: l_pStream->Size = %d", __FUNC__, m_pStream->Size);
LogMessage(LOG_DEBUG, l_Msg);
}
l_Bytes = m_pStream->Read(m_ReceiveBuf, sizeof(m_ReceiveBuf)-1);
if (0 == l_Bytes) // no data available end
break;
TCPAckFlag = true;
}
catch (const Exception &E)
{
if (!s_Exception_printed1)
{
s_Exception_printed1 = 1;
#pragma warn -8037
LogMessage(LOG_ERROR, AnsiString(E.ClassName())+ E.Message);
if(E.ClassNameIs("ESocketError"))
#pragma warn +8037
{
// DWORD l_Error = GetLastError();
break;
}
}
}
catch (...)
{
if (!s_Exception_printed)
{
l_Msg.printf("%s: caugth exception on WaitForData/Read", __FUNC__);
LogMessage(LOG_ERROR, l_Msg);
s_Exception_printed = 1;
}
continue;
}
if (l_Bytes == sizeof(m_ReceiveBuf)-1)
{
if (!m_InSidePacket)
{
if (TCP_STX == *m_ReceiveBuf)
{
// STX inside packet -- old packet is invalid - discard
// and start new receive
InitBuffer();
}
else
{
switch (*m_ReceiveBuf)
{
case TCP_ENQ:
Snd_ACK();
break;
case TCP_ACK: // ACK
l_Msg.printf("%s: (%s/%d) ACK received", __FUNC__,
m_ClientSocket->Address, m_ClientSocket->Port);
LogMessage(LOG_DEBUG, l_Msg);
if (m_SetupWaitForAck)
{
// Synchronize(StartThreads);
SetEvent(g_EvSetupOK);
m_SetupWaitForAck = false;
}
break;
case TCP_NAK: // NAK
l_Msg.printf("%s: (%s/%d) NACK received", __FUNC__,
m_ClientSocket->Address, m_ClientSocket->Port);
LogMessage(LOG_DEBUG, l_Msg);
break;
default:
// discard data
l_Msg.printf("%s: (%s/%d) discarding data >%x<", __FUNC__,
m_ClientSocket->Address, m_ClientSocket->Port,
(int) (*m_ReceiveBuf & 0377));
LogMessage(LOG_DEBUG, l_Msg);
}
}
}
else // now we are inside a packet
{
if (TCP_STX == *m_ReceiveBuf)
{
// start receive
InitBuffer();
}
else
{
if (TCP_ETX == *m_ReceiveBuf)
{
// end of data wait for bcc
m_GotEtx = true;
}
else
{
if (m_GotEtx)
{
// calculate bcc
m_Bcc = '\0';
for (char* p = m_ReceivedBlock; p != m_pReceive; p++)
m_Bcc ^= *p;
// compare against value
if (m_Bcc == ((unsigned char) *m_ReceiveBuf))
{
// have a valid block - do something with it
m_Length = m_pReceive - m_ReceivedBlock;
Synchronize(ProcessData);
m_InSidePacket = false;
}
else
{
l_Msg.printf("%s: bcc mismatch exp: %x rec: %x", __FUNC__,
(int) (m_Bcc & 0377), (int) (*m_ReceiveBuf & 0377));
LogMessage(LOG_PRODUCTION, l_Msg);
}
}
else
{
// normal packet data - store
*m_pReceive++ = *m_ReceiveBuf;
}
}
}
}
}
}
}
__finally
{
delete m_pStream;
}
}
#####
THX
Andreas Tirok
"Andreas Tirok" <at1712@web.de> schrieb im Newsbeitrag
news:4125bdc6$1@newsgroups.borland.com...
> Hi @all,
>
> is there anywhere an example of TClientSocket in blocking mode.
> I run into some problems and want to look at the code ...
>
> THX
>
> Andreas Tirok
>
>
Technical Details
Message-ID:
<4125db6b@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!npeer.de.kpn-eurorings.net!feed.news.schlund.de!schlund.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4125bdc6$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,b0259efaad300f7e X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII Reply-To: "Andreas Tirok" <at1712@web.de> From: "Andreas Tirok" <at1712@web.de> Newsgroups: borland.public.cppbuilder.internet.socket References: <4125bdc6$1@newsgroups.borland.com> Subject: Re: TClientSocket in blocking mode ... Date: Fri, 20 Aug 2004 13:07:18 +0200 Lines: 276 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 NNTP-Posting-Host: 80.140.210.115 Message-ID: <4125db6b@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1093000043 80.140.210.115 (20 Aug 2004 04:07:23 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!npeer.de.kpn-eurorings.net!feed.news.schlund.de!schlund.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail