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

#include <vcl.h>
#pragma hdrstop

#include "frmServerSocket.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::_listenClick(TObject *Sender)
{
	if(!this->ServerSocket1->Active) {
		this->ServerSocket1->Port = this->_listenPort->Text.ToInt();
		this->ServerSocket1->Open();
	}
	this->_listen->Enabled = false;
	this->_disconn->Enabled = true;
	this->_Send->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::_disconnClick(TObject *Sender)
{
	if(this->ServerSocket1->Active) {
		for (int i = 0; i < this->ServerSocket1->Socket->ActiveConnections; i++) {
			ServerSocket1->Socket->Connections[i]->Close();
		}  
		this->ServerSocket1->Close();

	}

	this->_listen->Enabled = true;
	this->_disconn->Enabled = false;
	this->_Send->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientConnect(TObject *Sender, TCustomWinSocket *Socket)

{
	UnicodeString us;
	us = Socket->RemoteAddress;
	this->ListBox1->Items->Add(us);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::_SendClick(TObject *Sender)
{
	for (int i = 0; i < this->ServerSocket1->Socket->ActiveConnections; i++) {
			int hexBufLen = _sendBuf->Text.Length()/2;
			char *buf = 0;
	
			if (this->CheckBox1->Checked) {
				buf = new char[hexBufLen];
				HexToBin(_sendBuf->Text.c_str(),buf,hexBufLen);
				ServerSocket1->Socket->Connections[i]->SendBuf(buf,hexBufLen);
				delete [] buf;
			}
			else {
				ServerSocket1->Socket->Connections[i]->SendBuf(_sendBuf->Text.c_str(),_sendBuf->Text.Length()*sizeof(wchar_t));
			}  
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender, TCustomWinSocket *Socket)
          
{  
	
		UnicodeString us;
		int len = Socket->ReceiveLength();
		//int hexLen = len *2;
		char *buf = new char[len];
		char *hexBuf = new char[len*2];
		Socket->ReceiveBuf(buf,len);
		us = UnicodeString((wchar_t*)buf,len/2);
		this->_stringMemo->Lines->Add(us);
		BinToHex(buf, hexBuf, len); // len is for buf
		us = UnicodeString(hexBuf,len*2);
		this->_hexMemo->Lines->Add(us);
		delete [] buf;
		delete [] hexBuf;
 
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	this->_stringMemo->Lines->Clear();
	this->_hexMemo->Lines->Clear();	
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ServerSocket1ClientError(TObject *Sender, TCustomWinSocket *Socket,
          TErrorEvent ErrorEvent, int &ErrorCode)
{
	ErrorCode = 0;	
}
//---------------------------------------------------------------------------
