🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
5 messages
5 total messages Started by Andreas Tirok Fri, 20 Aug 2004 09:00
TClientSocket in blocking mode ...
#5359
Author: Andreas Tirok
Date: Fri, 20 Aug 2004 09:00
8 lines
1022 bytes
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
Re: TClientSocket in blocking mode ...
#5360
Author: Andreas Tirok
Date: Fri, 20 Aug 2004 11:07
274 lines
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
>
>
Re: TClientSocket in blocking mode ...
#5362
Author: Remy Lebeau (TeamB)
Date: Fri, 20 Aug 2004 18:17
16 lines
1371 bytes
"Andreas Tirok" <at1712@web.de> wrote in message
news:4125db6b@newsgroups.borland.com...

>     if (m_pStream->Size > 0)

The Size property has no meaning to TWinSocketStream at all, it is not
implemented and always return 0.  The only thing you can do is Read() from
the socket and see how many bytes were actually read.

>     l_Bytes = m_pStream->Read(m_ReceiveBuf, sizeof(m_ReceiveBuf)-1);

Where and how is m_ReceiveBuf declared?


Gambit
Re: TClientSocket in blocking mode ...
#5366
Author: Andreas Tirok
Date: Sun, 22 Aug 2004 09:23
43 lines
2137 bytes
Hello Gambit,

"Remy Lebeau (TeamB)" <gambit47.no.spam@no.spam.yahoo.com> schrieb im
Newsbeitrag news:41263fe9$1@newsgroups.borland.com...
>
> "Andreas Tirok" <at1712@web.de> wrote in message
> news:4125db6b@newsgroups.borland.com...
>
> >     if (m_pStream->Size > 0)
>
> The Size property has no meaning to TWinSocketStream at all, it is not
> implemented and always return 0.  The only thing you can do is Read() from
> the socket and see how many bytes were actually read.

Hmmm, I didn't got this from the documentation ... grrrr

>
> >     l_Bytes = m_pStream->Read(m_ReceiveBuf, sizeof(m_ReceiveBuf)-1);
>
> Where and how is m_ReceiveBuf declared?

It is a class member - all variables starting with m_ are class members.

class SecomClientReadThread : public TThread
{
 private:
  bool    m_InSidePacket;
  bool    m_GotEtx;
  unsigned char  m_Bcc;
  char    m_ReceiveBuf[2];
  char    m_ReceivedBlock[10240];
  char    *m_pReceive;
  int     m_Length;

.....
};

Hmmm, is it possible, that the server and the client - when both running in
blocking mode - can deadlock?

Thank you and Regards.

Andreas Tirok
Re: TClientSocket in blocking mode ...
#5373
Author: Mark Jacobs
Date: Tue, 24 Aug 2004 11:09
27 lines
1768 bytes
On Fri, 20 Aug 2004 11:00:53 +0200, Andreas Tirok <at1712@web.de> wrote:

> is there anywhere an example of TClientSocket in blocking mode.

       static char bf[999999];
       ClientSocket1->Open();
       Application->ProcessMessages();
       csstream=new TWinSocketStream(ClientSocket1->Socket,60000);
       if (!csstream->WaitForData(5000))
         Memo1->Lines->Append("Nothing to read in 5 seconds");
       else
       {
         memset(bf,0,999998);
         csstream->Read(bf,999998);
         Memo1->SetSelTextBuf(bf);
       }
       // ... whatever else you want to do ...
       delete csstream;

HTH
-- 
Mark Jacobs
DK Computing
http://www.dkcomputing.co.uk
markj atty critical dotty co dotty uk
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads