//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TSCForm *SCForm1;
String buf ;
int s[6];
String d;
String timer;
int t=0;

TSCForm *SCForm;
//---------------------------------------------------------------------------
__fastcall TSCForm::TSCForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TSCForm::Listen1Click(TObject *Sender)
{
  Listen1->Checked=!Listen1->Checked;
  if(Listen1->Checked)
  {ClientSocket1->Active=false;
   ServerSocket1->Active=true;
   StatusBar1->Panels->Items[0]->Text="Listening...";
  }else
  {if(ServerSocket1->Active)
   {ServerSocket1->Active=false;}
   StatusBar1->Panels->Items[0]->Text="";
  }
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::Connect1Click(TObject *Sender)
{
  if(ClientSocket1->Active)
  {ClientSocket1->Active=false;}
  if(InputQuery("Computer to connect to","Address Name:", Server))
  {if(Server.Length()>0)
    {ClientSocket1->Host=Server;
     ClientSocket1->Active=true;
     Listen1->Checked=false;
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::Exit1Click(TObject *Sender)
{
  ServerSocket1->Close();
  ClientSocket1->Close();
  Close();
}
//---------------------------------------------------------------------------

void __fastcall TSCForm::Memo1KeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
  if(Key==VK_RETURN)
  {
  if(IsServer){ServerSocket1->Socket->Connections[0]->SendText(Memo1->Lines->Strings[Memo1->Lines->Count -1]);}
  else{ClientSocket1->Socket->SendText(Memo1->Lines->Strings[Memo1->Lines->Count -1]);}
  }
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::FormCreate(TObject *Sender)
{
  Listen1Click(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::ClientSocket1Connect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  StatusBar1->Panels->Items[0]->Text="Connect to:" +Socket->RemoteHost;
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::Disconnect1Click(TObject *Sender)
{
  ClientSocket1->Active=false;
  ServerSocket1->Active=true;
  StatusBar1->Panels->Items[0]->Text="Listening...";
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{
  Memo2->Lines->Add(Socket->ReceiveText());        
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::ServerSocket1ClientRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
  Memo2->Lines->Add(Socket->ReceiveText());       
  Memo1->Lines->Add(buf);
   if(buf=="ECHO")
   {
        Socket->SendText(d);
        Memo1->Lines->Add(d);
   }
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::ServerSocket1Accept(TObject *Sender,
      TCustomWinSocket *Socket)
{
  IsServer=true;
  StatusBar1->Panels->Items[0]->Text="Connect to:"+Socket->RemoteAddress;        
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::ServerSocket1ClientConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  Memo2->Lines->Clear();        
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::ClientSocket1Disconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  Listen1Click(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::ClientSocket1Error(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
  Memo2->Lines->Add("Error connecting to:"+Server);
  ErrorCode=0;        
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::ServerSocket1ClientDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
  StatusBar1->Panels->Items[0]->Text="Listening...";        
}
//---------------------------------------------------------------------------
void __fastcall TSCForm::Open1Click(TObject *Sender)
{
      if(OpenDialog1->Execute())
      {
      Memo1->Lines->LoadFromFile(OpenDialog1->FileName);
      }
}
//---------------------------------------------------------------------------

void __fastcall TSCForm::Save1Click(TObject *Sender)
{
  if(SaveDialog1->Execute())
  {
  Memo2->Lines->SaveToFile(SaveDialog1->FileName);
  }
}
//---------------------------------------------------------------------------







