//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------


__fastcall TMyServerThread::TMyServerThread(bool CreateSuspended, TServerClientWinSocket* ASocket)
           : TServerClientThread(CreateSuspended, ASocket)
           {   }


void __fastcall TMyServerThread::ClientExecute()
{

  while( !Terminated && ClientSocket->Connected)
  {
     try
     {
       TWinSocketStream *pStream = new TWinSocketStream(ClientSocket,60000);
      try
      {
       char buffer[10];
       memset(buffer,0,sizeof(buffer));
       if (pStream->WaitForData(60000) )
       {
         if (pStream->Read(&ReceiveBuf,sizeof(ReceiveBuf)) == 0)
           ClientSocket->Close();
         // Put stuff in the right places...
         Form1->ReceivedFloat->Text = AnsiString::FloatToStrF(ReceiveBuf.DLValue,AnsiString::sffGeneral,15,1);
         Form1->ReceivedInt->Text = AnsiString::FloatToStrF(ReceiveBuf.IntValue,AnsiString::sffGeneral,5,1);

       } // end if
       else
         ClientSocket->Close();
      } // end try.
      __finally
      {
        delete pStream;
      }

     } // end try
     catch( ...)
     {

      HandleException();
     }


  } // end while.

}

void __fastcall TForm1::ServerSocket1GetThread(TObject *Sender,
      TServerClientWinSocket *ClientSocket,
      TServerClientThread *&SocketThread)
{
  SocketThread = new TMyServerThread(false,ClientSocket);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::SendButtonClick(TObject *Sender)
{
  SendBuf.DLValue = SentFloat->Text.ToDouble();
  SendBuf.IntValue = SentInt->Text.ToInt();
  ClientSocket1->Active = true;
  ClientSocket1->Socket->SendBuf( &SendBuf, sizeof(SendBuf));
  ClientSocket1->Active = false;
}
//---------------------------------------------------------------------------

