#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ScktComp.hpp>
//---------------------------------------------------------------------------
// Wait 60 seconds for client
const int CLIENTWAITTIME = 60000;
// Size of buffer for reading/writing text across socket connection
const int BUFFERSIZE = 32;
class TForm1 : public TForm
{
__published: // IDE-managed Components
TServerSocket *ServerSocket1;
TMemo *Memo1;
void __fastcall ServerSocket1GetThread(TObject *Sender,
TServerClientWinSocket *ClientSocket,
TServerClientThread *&SocketThread);
void __fastcall ServerSocket1Accept(TObject *Sender,
TCustomWinSocket *Socket);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Threads for server connections are descendants of TServerClientThread.
// Thus, you may not use the New Thread object dialog.
// Instead, declare your thread manually as follows:
class PACKAGE TMyServerThread : public Scktcomp::TServerClientThread
{
public:
// if true, FreeOnTerminate is set to false before the thread terminates,
// and the thread is left in the thread cache. When KeepInCache is false,
// the thread is freed when execution terminates.
__fastcall TMyServerThread(bool CreateSuspended, TServerClientWinSocket* ASocket)
: Scktcomp::TServerClientThread(CreateSuspended, ASocket)
{ CreateSuspended = false; KeepInCache=true; FreeOnTerminate=false; };
// To implement this thread, you override the ClientExecute method instead of the Execute method.
void __fastcall ClientExecute(void);
};
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TMyServerThread::ClientExecute(void)
{
// make sure connection is active
while (!Terminated && ClientSocket->Connected)
{
try
{
// Now, use TWinSocketStream to read or write information
// over a blocking socket connection
TWinSocketStream *pStream = new TWinSocketStream(ClientSocket, CLIENTWAITTIME);
// give the client 60 seconds to start writing
if (pStream->WaitForData(CLIENTWAITTIME))
{
if (pStream->Read(buffer, sizeof(buffer)) == 0)
// (if can't read in 60 seconds) than close the connection
ClientSocket->Close();
else
{
// Client to Server test text
Form1->Memo1->Lines->Add(AnsiString("(Client) ") +AnsiString(buffer) );
// Back again to Client
pStream->Write( buffer, sizeof(buffer));
}